R
Railway•13mo ago
ZeroxAdvanced

NiceGUI deployment to Railway help with dockerfile

Hi I hope someone can help me. So far I am loving Railway. I already have a Django project and it runs fine. Now I am using nicegui to build an app but I cannot get the deployment to Railway working. App Code is mostly in main.py How to deploy my nicegui app to railway? I created a dockerfile and it seems succesfully running but I think uvicorn is not running? Code is below: # Pull the official base image FROM python:3.10-slim-buster # Set environment variables ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # The port environment variable is set by Railway when the container is run # But we can default it to 8080 for local development ENV PORT=8080 # Expose the port EXPOSE $PORT # Set work directory in the container WORKDIR /code # Install dependencies COPY requirements.txt /code/ RUN pip install --upgrade pip RUN pip install -r requirements.txt # Copy project COPY . /code/ # Command to run on container start CMD ["python", "main.py"] In the console I get the feed: NiceGUI ready to go on http://localhost:8080, and http://172.17.2.235:8080 But i think i need to expose it with the uvicorn? Can someone help? project id: 3e62bf9f-3eee-4bbd-80e8-83d7d0e2acf1
39 Replies
Percy
Percy•13mo ago
Project ID: 3e62bf9f-3eee-4bbd-80e8-83d7d0e2acf1
Brody
Brody•13mo ago
is nicegui a fastapi app?
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
Hi yes it is fast api and just py i believe
Brody
Brody•13mo ago
do you have uvicorn in your requirements.txt file?
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
Need to check i believe it is.
Brody
Brody•13mo ago
in the main.py file, is the fastapi variable called app ?
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
No there is no app definition in the main.py it is not needed for niceGUI. See docs of nicegui it just runs with python main.py and no ref needed for an app variable
Brody
Brody•13mo ago
send me the docs please try setting a PORT service variable to 8080
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
hi i see on their site also this info You can run it if you only have main.py with docker run -it --rm -p 8888:8080 \ -v "$PWD":/app zauberzeug/nicegui i did in the docker file But we can default it to 8080 for local development ENV PORT=8080 It is running NiceGUI ready to go on http://localhost:8080/, and http://172.17.2.235:8080/ I think i have some ip mapping wrong or need to add extra line for uvicorn?
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
NiceGUI
NiceGUI is an easy-to-use, Python-based UI framework, which shows up in your web browser. You can create buttons, dialogs, Markdown, 3D scenes, plots and much more.
Brody
Brody•13mo ago
a service variable
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
ok how do i set the service variable?
Brody
Brody•13mo ago
in the variables tab
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
ok checking doesnt work let me check my requirements.txt aiofiles==23.2.1 aiohttp==3.8.6 aiosignal==1.3.1 annotated-types==0.6.0 anyio==3.7.1 async-timeout==4.0.3 attrs==23.1.0 bidict==0.22.1 certifi==2023.7.22 charset-normalizer==3.3.2 click==8.1.7 exceptiongroup==1.1.3 fastapi==0.104.1 fastapi-socketio==0.0.10 frozenlist==1.4.0 h11==0.14.0 httpcore==1.0.1 httptools==0.6.1 httpx==0.25.1 idna==3.4 ifaddr==0.2.0 itsdangerous==2.1.2 Jinja2==3.1.2 markdown2==2.4.10 MarkupSafe==2.1.3 multidict==6.0.4 nicegui==1.4.2 orjson==3.9.10 pscript==0.7.7 pydantic==2.4.2 pydantic_core==2.10.1 Pygments==2.16.1 python-dotenv==1.0.0 python-engineio==4.8.0 python-multipart==0.0.6 python-socketio==5.10.0 PyYAML==6.0.1 requests==2.31.0 simple-websocket==1.0.0 sniffio==1.3.0 starlette==0.27.0 typing_extensions==4.8.0 urllib3==2.0.7 uvicorn==0.22.0 uvloop==0.19.0 vbuild==0.8.2 watchfiles==0.21.0 websockets==12.0 wsproto==1.2.0 yarl==1.9.2
Brody
Brody•13mo ago
show me how you've set the service variable
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
PORT=8080 I always just use the raw data editer in Railway.
Brody
Brody•13mo ago
okay, then you have to get nicegui to listen on host 0.0.0.0 as well
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
yes how do i do that?
No description
Brody
Brody•13mo ago
you'd need to consult nicegui's docs for that
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
NiceGUI
NiceGUI is an easy-to-use, Python-based UI framework, which shows up in your web browser. You can create buttons, dialogs, Markdown, 3D scenes, plots and much more.
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
can you help? I think this is all the info we get
Brody
Brody•13mo ago
I will have a look, but please read the docs yourself as well
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
I did 🙂 The help is really appriciated🙏
Brody
Brody•13mo ago
try changing the CMD command in your dockerfile to
uvicorn main:app --host 0.0.0.0 --port $PORT
uvicorn main:app --host 0.0.0.0 --port $PORT
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
yes tried this already but there is no main:app defnition in my main.py as NiceGui doesnt require this. Basically i do this when creating a fast API app, but here its not required and i dont see it in the code examples
Brody
Brody•13mo ago
that's how it's done as per their example code for fastapi https://github.com/zauberzeug/nicegui/tree/main/examples%2Ffastapi
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
yes but already that, that was why i was consulting the discord. Perhaps i need to contact them
Brody
Brody•13mo ago
you need to run your nicegui with uvicorn like it's shown in their own example nicegui might not require it, but to run your nicegui app I'm production mode on Railway, it's required
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
GitHub
nicegui/examples/fastapi/frontend.py at main · zauberzeug/nicegui
Create web-based user interfaces with Python. The nice way. - zauberzeug/nicegui
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
i will do some rework on this and post the results here..need to add fastapi then separately
Brody
Brody•13mo ago
sounds good
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
ui.run_with( fastapi_app, storage_secret='pick your private secret here', # NOTE setting a secret is optional but allows for persistent storage per user ) I see they using the run_with and then some parameters for Fast API..
Brody
Brody•13mo ago
though I don't see why you can't use uvicorn to run your nicegui app, the main.py file imports app, thus your main.py now has an app declaration
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
uvicorn main:app --host 0.0.0.0 --port $PORT will check again but i believe it tested it. will do it again and let you know ERROR: Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 677, in lifespan async with self.lifespan_context(app) as maybe_state: File "/usr/local/lib/python3.10/contextlib.py", line 199, in __aenter__ return await anext(self.gen) File "/usr/local/lib/python3.10/site-packages/nicegui/nicegui.py", line 29, in _lifespan _startup() File "/usr/local/lib/python3.10/site-packages/nicegui/nicegui.py", line 83, in _startup raise RuntimeError('\n\n' RuntimeError: You must call ui.run() to start the server. If ui.run() is behind a main guard if __name__ == "__main__": remove the guard or replace it with if __name__ in {"__main__", "__mp_main__"}: to allow for multiprocessing. ERROR: Application startup failed. Exiting. I think it must be configured in the main.app aswell as shown in the link provided earlier. Just changing the dockerfile with the CMD uvicorn doesnt help yet. Will let you know and tinker more
Brody
Brody•13mo ago
sounds good
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
Hi got it running Solution: You need to declare fast api in the project e.g. in main.py
from fastapi import FastAPI
from nicegui import events, ui

app = FastAPI()

# Create an init function which will be called with the FastAPI app
def init(fastapi_app: FastAPI) -> None:

@ui.page('/index')
def page_layout():
ui.label('Acces your orders easily')
[rest of your code]

# Run the NiceGUI with the FastAPI app instance and a secret for storage
ui.run_with(
fastapi_app,
storage_secret='pick your private secret here', # Choose a secure secret here
)

[at the end of your code]
# Finally, call the init function with the FastAPI app instance
init(app)
from fastapi import FastAPI
from nicegui import events, ui

app = FastAPI()

# Create an init function which will be called with the FastAPI app
def init(fastapi_app: FastAPI) -> None:

@ui.page('/index')
def page_layout():
ui.label('Acces your orders easily')
[rest of your code]

# Run the NiceGUI with the FastAPI app instance and a secret for storage
ui.run_with(
fastapi_app,
storage_secret='pick your private secret here', # Choose a secure secret here
)

[at the end of your code]
# Finally, call the init function with the FastAPI app instance
init(app)
After this you can just add the below dockerfile
# Pull the official base image
FROM python:3.10-slim-buster

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# The port environment variable is set by Railway when the container is run
# But we can default it to 8080 for local development
ENV PORT=8080

# Expose the port
EXPOSE $PORT

# Set work directory in the container
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Copy project
COPY . /code/

# Command to run on container start
#CMD ["python", "main.py"]

CMD uvicorn main:app --host 0.0.0.0 --port $PORT
# Pull the official base image
FROM python:3.10-slim-buster

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# The port environment variable is set by Railway when the container is run
# But we can default it to 8080 for local development
ENV PORT=8080

# Expose the port
EXPOSE $PORT

# Set work directory in the container
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Copy project
COPY . /code/

# Command to run on container start
#CMD ["python", "main.py"]

CMD uvicorn main:app --host 0.0.0.0 --port $PORT
Brody
Brody•13mo ago
would you mind enclosing these code blocks with triple backticks?
ZeroxAdvanced
ZeroxAdvancedOP•13mo ago
@Brody thanks for the help! You pointed me in the good direction with the example in the github code! I was able to solve it with some more code examples. Hope this solution will help the railway community. Thanks again and have nice evening.
Brody
Brody•13mo ago
it definitely will, thanks for coming back to report the final solution!
Want results from more Discord servers?
Add your server