Skip to content

Commit

Permalink
#469 improved fastapi example and docs
Browse files Browse the repository at this point in the history
We should always use workers == 1
  • Loading branch information
rodja committed Mar 18, 2023
1 parent 7c7bda4 commit ed0ae76
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/fastapi/frontend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import FastAPI

from nicegui import ui


Expand Down
3 changes: 1 addition & 2 deletions examples/fastapi/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
import frontend
import uvicorn
from fastapi import FastAPI

app = FastAPI()
Expand All @@ -14,4 +13,4 @@ def read_root():
frontend.init(app)

if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)
print('Please start the app with the "uvicorn" command as shown in the start.sh script')
23 changes: 21 additions & 2 deletions examples/fastapi/start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
#!/usr/bin/env bash

# Start the FastAPI app
uvicorn main:app --reload
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <prod|dev>"
exit 1
fi

# use path of this demo as working directory; enables starting this script from anywhere
cd "$(dirname "$0")"

if [ "$1" = "prod" ]; then
echo "Starting Uvicorn server in production mode..."
# we also use a single worker in production mode so socket.io connections are always handled by the same worker
uvicorn main:app --workers 1 --log-level info --port 80
elif [ "$1" = "dev" ]; then
echo "Starting Uvicorn server in development mode..."
# reload implies workers = 1
uvicorn main:app --reload --log-level debug --port 8080
else
echo "Invalid parameter. Use 'prod' or 'dev'."
exit 1
fi

3 changes: 2 additions & 1 deletion website/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,8 +1028,9 @@ def env_var_example():
In production we also like using reverse proxies like [Traefik](https://doc.traefik.io/traefik/) or [NGINX](https://www.nginx.com/) to handle these details for us.
See our [docker-compose.yml](https://github.com/zauberzeug/nicegui/blob/main/docker-compose.yml) as an example.
You may also have a look at [our example for using a custom FastAPI app](https://github.com/zauberzeug/nicegui/tree/main/examples/fastapi).
You may also have a look at [our demo for using a custom FastAPI app](https://github.com/zauberzeug/nicegui/tree/main/examples/fastapi).
This will allow you to do very flexible deployments as described in the [FastAPI documentation](https://fastapi.tiangolo.com/deployment/).
Note that there are additional steps required to allow multiple workers.
''')

with ui.column().classes('w-full mt-8 arrow-links'):
Expand Down

0 comments on commit ed0ae76

Please sign in to comment.