Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve UI for fastapi dev and fastapi run #95

Merged
merged 9 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 27 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,33 @@ To run your FastAPI app for development, you can use the `fastapi dev` command:

```console
$ fastapi dev main.py
INFO Using path main.py
INFO Resolved absolute path /home/user/code/awesomeapp/main.py
INFO Searching for package file structure from directories with __init__.py files
INFO Importing from /home/user/code/awesomeapp

╭─ Python module file ─╮
│ │
│ 🐍 main.py │
│ │
╰──────────────────────╯

INFO Importing module main
INFO Found importable FastAPI app

╭─ Importable FastAPI app ─╮
│ │
│ from main import app │
│ │
╰──────────────────────────╯

INFO Using import string main:app

╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: http://127.0.0.1:8000 │
│ │
│ API docs: http://127.0.0.1:8000/docs │
│ │
│ Running in development mode, for production use: │
│ │
│ fastapi run │
│ │
╰─────────────────────────────────────────────────────╯

INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [56345] using WatchFiles
INFO: Started server process [56352]
INFO: Waiting for application startup.
INFO: Application startup complete.

FastAPI Starting development server 🚀

Searching for package file structure from directories with __init__.py files
Importing from /home/user/code/awesomeapp

module 🐍 main.py

code Importing the FastAPI app object from the module with the following code:

from main import app

app Using import string: main:app

server Server started at http://127.0.0.1:8000
server Documentation at http://127.0.0.1:8000/docs

tip Running in development mode, for production use: fastapi run

Logs:

INFO Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO Started reloader process [4106097] using WatchFiles
INFO Started server process [4106120]
INFO Waiting for application startup.
INFO Application startup complete.
```

</div>
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ classifiers = [
dependencies = [
"typer >= 0.12.3",
"uvicorn[standard] >= 0.15.0",
"rich-toolkit >= 0.11.1"
]

[project.optional-dependencies]
Expand Down
157 changes: 113 additions & 44 deletions src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from logging import getLogger
import logging
from pathlib import Path
from typing import Any, Union
from typing import Any, List, Union

import typer
from rich import print
from rich.padding import Padding
from rich.panel import Panel
from rich.tree import Tree
from typing_extensions import Annotated

from fastapi_cli.discover import get_import_string
from fastapi_cli.discover import get_import_data
from fastapi_cli.exceptions import FastAPICLIException

from . import __version__
from .logging import setup_logging
from .utils.cli import get_rich_toolkit, get_uvicorn_log_config

app = typer.Typer(rich_markup_mode="rich")

setup_logging()
logger = getLogger(__name__)
logger = logging.getLogger(__name__)


try:
import uvicorn
Expand All @@ -39,6 +39,7 @@ def callback(
"--version", help="Show the version and exit.", callback=version_callback
),
] = None,
verbose: bool = typer.Option(False, help="Enable verbose output"),
) -> None:
"""
FastAPI CLI - The [bold]fastapi[/bold] command line app. 😎
Expand All @@ -48,6 +49,31 @@ def callback(
Read more in the docs: [link=https://fastapi.tiangolo.com/fastapi-cli/]https://fastapi.tiangolo.com/fastapi-cli/[/link].
"""

log_level = logging.DEBUG if verbose else logging.INFO

setup_logging(level=log_level)


def _get_module_tree(module_paths: List[Path]) -> Tree:
root = module_paths[0]
name = f"🐍 {root.name}" if root.is_file() else f"📁 {root.name}"

root_tree = Tree(name)

if root.is_dir():
root_tree.add("[dim]🐍 __init__.py[/dim]")

tree = root_tree
for sub_path in module_paths[1:]:
sub_name = (
f"🐍 {sub_path.name}" if sub_path.is_file() else f"📁 {sub_path.name}"
)
tree = tree.add(sub_name)
if sub_path.is_dir():
tree.add("[dim]🐍 __init__.py[/dim]")

return root_tree


def _run(
path: Union[Path, None] = None,
Expand All @@ -61,45 +87,88 @@ def _run(
app: Union[str, None] = None,
proxy_headers: bool = False,
) -> None:
try:
use_uvicorn_app = get_import_string(path=path, app_name=app)
except FastAPICLIException as e:
logger.error(str(e))
raise typer.Exit(code=1) from None
url = f"http://{host}:{port}"
url_docs = f"{url}/docs"
serving_str = f"[dim]Serving at:[/dim] [link={url}]{url}[/link]\n\n[dim]API docs:[/dim] [link={url_docs}]{url_docs}[/link]"

if command == "dev":
panel = Panel(
f"{serving_str}\n\n[dim]Running in development mode, for production use:[/dim] \n\n[b]fastapi run[/b]",
title="FastAPI CLI - Development mode",
expand=False,
padding=(1, 2),
style="black on yellow",
with get_rich_toolkit() as toolkit:
server_type = "development" if command == "dev" else "production"

toolkit.print_title(f"Starting {server_type} server 🚀", tag="FastAPI")
toolkit.print_line()

toolkit.print(
"Searching for package file structure from directories with [blue]__init__.py[/blue] files"
)

try:
import_data = get_import_data(path=path, app_name=app)
except FastAPICLIException as e:
toolkit.print_line()
toolkit.print(f"[error]{e}")
raise typer.Exit(code=1) from None

logger.debug(f"Importing from {import_data.module_data.extra_sys_path}")
logger.debug(f"Importing module {import_data.module_data.module_import_str}")

module_data = import_data.module_data
import_string = import_data.import_string

toolkit.print(f"Importing from {module_data.extra_sys_path}")
toolkit.print_line()

root_tree = _get_module_tree(module_data.module_paths)

toolkit.print(root_tree, tag="module")
toolkit.print_line()

toolkit.print(
"Importing the FastAPI app object from the module with the following code:",
tag="code",
)
else:
panel = Panel(
f"{serving_str}\n\n[dim]Running in production mode, for development use:[/dim] \n\n[b]fastapi dev[/b]",
title="FastAPI CLI - Production mode",
expand=False,
padding=(1, 2),
style="green",
toolkit.print_line()
toolkit.print(
f"[underline]from [bold]{module_data.module_import_str}[/bold] import [bold]{import_data.app_name}[/bold]"
)
toolkit.print_line()

toolkit.print(
f"Using import string: [blue]{import_string}[/]",
tag="app",
)

url = f"http://{host}:{port}"
url_docs = f"{url}/docs"

toolkit.print_line()
toolkit.print(
f"Server started at [link={url}]{url}[/]",
f"Documentation at [link={url_docs}]{url_docs}[/]",
tag="server",
)

if command == "dev":
toolkit.print_line()
toolkit.print(
"Running in development mode, for production use: [bold]fastapi run[/]",
tag="tip",
)

if not uvicorn:
raise FastAPICLIException(
"Could not import Uvicorn, try running 'pip install uvicorn'"
) from None

toolkit.print_line()
toolkit.print("Logs:")
toolkit.print_line()

uvicorn.run(
app=import_string,
host=host,
port=port,
reload=reload,
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
log_config=get_uvicorn_log_config(),
)
print(Padding(panel, 1))
if not uvicorn:
raise FastAPICLIException(
"Could not import Uvicorn, try running 'pip install uvicorn'"
) from None
uvicorn.run(
app=use_uvicorn_app,
host=host,
port=port,
reload=reload,
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
)


@app.command()
Expand Down
Loading
Loading