-
-
Notifications
You must be signed in to change notification settings - Fork 620
fly.io Deployment
This tutorial demonstrates how to deploy a NiceGUI application on Fly.io.
Create a Python file main.py
with the following content:
import os
from nicegui import ui
from quotes import Quotes
quotes = Quotes()
ui.button('Quote', on_click=lambda: ui.notify(quotes.random()[1]))
ui.run(reload='FLY_ALLOC_ID' not in os.environ)
The ui.run
parameter reload='FLY_ALLOC_ID' not in os.environ
will disable auto-reloading when deployed on fly.io.
This is crucial because you do not want your production server to restart if some files change.
Create a Dockerfile
which describes the setup of your app:
FROM zauberzeug/nicegui:1.3.13
RUN pip install --no-cache-dir quotes
COPY . /app
It's good practice to define a concrete version number instead of zauberzeug/nicegui:latest, to ensure you do not run into version conflicts if you deploy again in a few weeks (and latest
may have changed).
- Install flyctl, the command line interface (CLI) to administer your fly.io deployment.
- Create an account with the terminal command
fly auth signup
or login withfly auth login
. - Run
fly launch
from inside your project source directory. It will ask for a unique app name and some other information to create, configure, and deploy your new application.
The fly launch
command creates a fly.toml
file which describes your configuration and should exist in your project root directory under version control.
It should look similar to this:
app = "my-test-app-name"
primary_region = "fra"
[build]
dockerfile="Dockerfile"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
Run fly deploy
from inside your project source directory.
Your app is ready to access on https://my-test-app-name.fly.dev/, but of course with your unique app name.
The deployment will only work as expected with a single instance (per region). To allow load balancing you need to have something like sticky sessions and shared data storage; both not trivial to implement on fly.io. See https://github.com/zauberzeug/fly_fastapi_socketio for some guidance.