Spaces:
Sleeping
Sleeping
File size: 1,795 Bytes
036b3a6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# Live Reloading
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
When building your app it can be useful to view your changes in a web
browser as you make them. FastHTML supports live reloading which means
that it watches for any changes to your code and automatically refreshes
the webpage in your browser.
To enable live reloading simply replace
[`FastHTML`](https://www.fastht.ml/docs/api/core.html#fasthtml) in your
app with `FastHTMLWithLiveReload`.
``` python
from fasthtml.common import *
app = FastHTMLWithLiveReload()
```
Then in your terminal run `uvicorn` with reloading enabled.
uvicorn main:app --reload
**⚠️ Gotchas** - A reload is only triggered when you save your
changes. - `FastHTMLWithLiveReload` should only be used during
development. - If your app spans multiple directories you might need to
use the `--reload-dir` flag to watch all files in each directory. See
the uvicorn [docs](https://www.uvicorn.org/settings/#development) for
more info. - The live reload script is only injected into the page when
rendering [ft
components](https://www.fastht.ml/docs/explains/explaining_xt_components.html).
## Live reloading with `fast_app`
In development the `fast_app` function provides the same functionality.
It instantiates the `FastHTMLWithLiveReload` class if you pass
`live=True`:
<div class="code-with-filename">
**main.py**
``` python
from fasthtml.common import *
app, rt = fast_app(live=True)
serve()
```
</div>
Line 3
`fast_app()` instantiates the `FastHTMLWithLiveReload` class.
Line 5
[`serve()`](https://www.fastht.ml/docs/api/core.html#serve) is a wrapper
around a `uvicorn` call.
To run `main.py` in live reload mode, just do `python main.py`. We
recommend turning off live reload when deploying your app to production.
|