:memo: Add docs for launching applications/files.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sebastián Ramírez · committed by GitHub · parents d1970da, bc4709c · Browse files · 6 files·+89 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+89 added−1 removed
- other:+89 added−1 removed
Provenance
- Commit
8456849- Tree
d6406bb- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d1970da- Parent 2
bc4709c
6 files changed+89 added−1 removed
modified:docs/src/app_dir/tutorial001.py+1 added-1 removed
5 unmodified lines6 7 8 def main():9 app_dir = typer.get_app_dir(APP_NAME)10- config_path = Path(app_dir) / "config.json"10+ config_path: Path = Path(app_dir) / "config.json"11 if not config_path.is_file():12 typer.echo("Config file doesn't exist yet")13 14 2 unmodified linesadded:docs/src/launch/tutorial001.py+10 added
1+import typer2+3+4+def main():5+ typer.echo("Opening Typer's docs")6+ typer.launch("https://typer.tiangolo.com")7+8+9+if __name__ == "__main__":10+ typer.run(main)added:docs/src/launch/tutorial002.py+21 added
1+from pathlib import Path2+3+import typer4+5+APP_NAME = "my-super-cli-app"6+7+8+def main():9+ app_dir = typer.get_app_dir(APP_NAME)10+ app_dir_path = Path(app_dir)11+ app_dir_path.mkdir(parents=True, exist_ok=True)12+ config_path: Path = Path(app_dir) / "config.json"13+ if not config_path.is_file():14+ config_path.write_text('{"version": "1.0.0"}')15+ config_file_str = str(config_path)16+ typer.echo("Opening config directory")17+ typer.launch(config_file_str, locate=True)18+19+20+if __name__ == "__main__":21+ typer.run(main)modified:docs/tutorial/app-dir.md+8 added
31 unmodified lines32333435363738394041424331 unmodified lines
And it will create a new `Path` object from that.
If you want a quick guide on using `Path()` you can check <a href="https://realpython.com/python-pathlib/" target="_blank">this post on Real Python</a> or <a href="https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/" target="_blank">this post by Trey Hunner</a>.
In the code above, we are also explicitly declaring `config_path` as having type `Path` to help the editor provide completion and type checks:
```Pythonconfig_path: Path = Path(app_dir) / "config.json"```
Otherwise it could think it's a sub-type (a `PurePath`) and stop providing completion for some methods.added:docs/tutorial/launch.md+48 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748You can launch applications from your CLI program with `typer.launch()`.
It will launch the appropriate application depending on the URL or file type you pass it:
```Python hl_lines="6"{!./src/launch/tutorial001.py!}```
Check it:
<div class="termy">
```console$ python main.py
Opening Typer docs
// Opens browser with Typer's docs```
</div>
## Locating a file
You can also make the operating system open the file browser indicating where a file is located with `locate=True`:
```Python hl_lines="17"{!./src/launch/tutorial002.py!}```
!!! tip The rest of the code in this example is just making sure the app directory exists and creating the config file. But the most important part is the `typer.launch(config_file_str, locate=True)` with the argument `locate=True`.
Check it:
<div class="termy">
```console$ python main.py
Opening config directory
// Opens a file browser indicating where the config file is located```
</div>modified:mkdocs.yml+1 added
48 unmodified lines49505152535455565734 unmodified lines48 unmodified lines - File: 'tutorial/parameter-types/file.md' - Ask with Prompt: 'tutorial/prompt.md' - Progress Bar: 'tutorial/progressbar.md' - CLI Application Directory: 'tutorial/app-dir.md' - Launching Applications: 'tutorial/launch.md' - Alternatives, Inspiration and Comparisons: 'alternatives.md' - Help Typer - Get Help: 'help-typer.md' - Development - Contributing: 'contributing.md' - Release Notes: release-notes.md34 unmodified lines