Skip to content

Commit 0925f48

Browse files
committed
Add examples/webapp/README.md
1 parent 67a15bd commit 0925f48

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

examples/webapp/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# REST-RPC – Web App Example
2+
3+
This is a small example for a web app that is written with the help of REST-RPC and [PyScript](https://pyscript.net/).
4+
5+
Here, the complete front-end is located inside the `static` directory. The front-end logic is inside `static/main.py`.
6+
7+
Inside `./main.py`, you'll find that the FastAPI app that is auto-generated from REST-RPC via `api_impl.make_fastapi()` is re-routed to `/api` and that `/` points to the `static` directory. This means that you'll find the `read_item` method under `/api/items/{item_id}` instead of simply `/items/{item_id}`. This has to be considered by the front-end: Here, we are using `ApiClient(..., base_url="/api/")`.
8+
9+
> [!CAUTION]
10+
> This example takes multiple shortcuts that are not suited for production use.
11+
>
12+
> For example, the `rest_rpc` module is served statically here. This is not a good idea in general for multiple reasons. One of them is that the `static/rest_rpc/__pycache__` directory will be served statically and the containing files contain sensitive information.
13+
14+
Since `rest_rpc` was symlinked here for simplicity, the dependency management is not handled by PyScript. This means that we need to explicitly depend on `pydantic` inside the `static/pyscript.toml`.
15+
16+
By the way, if you want to use Pyodide without Pyscript, you can do that too. In this example, you can see for yourself that you can also use `engine="pyodide"` and it will still work (since PyScript uses Pyodide internally here). So you can simply install REST-RPC inside your Pyodide environment.
17+
18+
## Usage
19+
20+
To start the FastAPI server, simply run
21+
```shell
22+
$ uv run fastapi dev
23+
```
24+
25+
You should now be able to open `http://127.0.0.1:8000` in your webbrowser and see the web app.
26+
27+
When clicking on the button, you should get an alert with the API response.
28+
29+
If the app is hanging in the "Loading..." screen, something might be wrong with your internet connection or with PyScript's CDN.
30+
31+
## Structure
32+
33+
- `my_api.py`: Api definition (shared between back-end and front-end)
34+
- `main.py`: FastAPI back-end
35+
- `static/index.html`: Front-end HTML
36+
- `static/main.py`: Front-end logic implemented with PyScript
37+

0 commit comments

Comments
 (0)