Skip to content
Rodja Trappe edited this page Apr 9, 2024 · 14 revisions

Deploying a NiceGUI App on Fly.io

This tutorial demonstrates how to deploy a NiceGUI application on Fly.io.

Step 1: Create a NiceGUI App

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.

Step 2: Dockerize App

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).

Step 3: Setup and Launch

  1. Install flyctl, the command line interface (CLI) to administer your fly.io deployment.
  2. Create an account with the terminal command fly auth signup or login with fly auth login.
  3. 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"]

Step 4: Deploy

Run fly deploy from inside your project source directory.

Step 5: Enjoy

Your app is ready to access on https://my-test-app-name.fly.dev/, but of course with your unique app name.

Next Steps

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.