Skip to content

Commit fd0360a

Browse files
update project metadata
1 parent 2e77f6d commit fd0360a

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

Diff for: packages/uvicorn-hmr/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# uvicorn-hmr
2+
3+
This package provides hot module reloading (HMR) for [`uvicorn`](https://github.com/encode/uvicorn).
4+
5+
It uses [`watchfiles`](https://github.com/samuelcolvin/watchfiles) to detect FS modifications,
6+
re-executes the corresponding modules and restart the server (in the same process).
7+
8+
Since the reload is on-demand and the server is not restarted on every request, it is much faster than the built-in `--reload` option provided by `uvicorn`.
9+
10+
## Installation
11+
12+
```sh
13+
pip install uvicorn-hmr
14+
```
15+
16+
## Usage
17+
18+
Replace
19+
20+
```sh
21+
uvicorn main:app --reload
22+
```
23+
24+
with
25+
26+
```sh
27+
uvicorn-hmr main:app
28+
```
29+
30+
> [!NOTE]
31+
> Since this package is a proof-of-concept yet, there is no configuration available. But contributions are welcome!
32+
33+
## Why?
34+
35+
1. Restarting process on every request is not always necessary, and is rather expensive. With `hmr`, 3-party packages imports are memoized.
36+
2. `hmr` track dependencies on runtime, and only rerun necessary modules. If you changed a python file not used by the server entrypoint, it won't be reloaded.
37+
38+
## What this package is not?
39+
40+
> [!CAUTION]
41+
> `hmr` are sometimes refer to a feature that updates the page in the browser on the client side when the server code changes. This is not that. This package is a server-side HMR, that reloads the server code when it changes.

Diff for: packages/uvicorn-hmr/pyproject.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[project]
22
name = "uvicorn-hmr"
3-
version = "0.0.2"
3+
version = "0.0.2.1"
4+
readme = "README.md"
45
dependencies = [
56
"hmr~=0.1.2",
67
"typer-slim~=0.15.1",
@@ -10,6 +11,9 @@ dependencies = [
1011
[project.scripts]
1112
uvicorn-hmr = "uvicorn_hmr:app"
1213

14+
[project.urls]
15+
Homepage = "https://github.com/promplate/hmr"
16+
1317
[build-system]
1418
requires = ["pdm-backend"]
1519
build-backend = "pdm.backend"

Diff for: packages/uvicorn-hmr/uvicorn_hmr.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
from pathlib import Path
22
from typing import TYPE_CHECKING
33

4-
from typer import Argument, Typer
4+
from typer import Argument, Typer, secho
55

66
app = Typer(help="Hot Module Replacement for Uvicorn", add_completion=False)
77

88

99
@app.command(no_args_is_help=True)
1010
def main(slug: str = Argument("main:app"), reload_include: str = str(Path.cwd()), reload_exclude: str = ".venv"):
11+
if ":" not in slug:
12+
secho("Invalid slug: ", fg="red", nl=False)
13+
secho(slug, fg="yellow")
14+
exit(1)
1115
module, attr = slug.split(":")
1216

1317
fragment = Path(module.replace(".", "/"))
1418

1519
if (path := fragment.with_suffix(".py")).is_file() or (path := fragment / "__init__.py").is_file():
1620
file = path.resolve()
1721
else:
18-
raise FileNotFoundError(f"Module `{module}` not found")
22+
secho("Module", fg="red", nl=False)
23+
secho(f" {module} ", fg="yellow", nl=False)
24+
secho("not found.", fg="red")
25+
exit(1)
1926

2027
import sys
2128
from atexit import register

0 commit comments

Comments
 (0)