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-83d7d0e2acf139 Replies
Project ID:
3e62bf9f-3eee-4bbd-80e8-83d7d0e2acf1
is nicegui a fastapi app?
Hi yes it is fast api and just py i believe
do you have uvicorn in your requirements.txt file?
Need to check i believe it is.
in the main.py file, is the fastapi variable called
app
?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
send me the docs please
try setting a
PORT
service variable to 8080
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?
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.
a service variable
ok how do i set the service variable?
in the variables tab
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
show me how you've set the service variable
PORT=8080
I always just use the raw data editer in Railway.
okay, then you have to get nicegui to listen on host 0.0.0.0 as well
yes how do i do that?
you'd need to consult nicegui's docs for that
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.
can you help? I think this is all the info we get
I will have a look, but please read the docs yourself as well
I did 🙂 The help is really appriciated🙏
try changing the CMD command in your dockerfile to
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
that's how it's done as per their example code for fastapi
https://github.com/zauberzeug/nicegui/tree/main/examples%2Ffastapi
yes but already that, that was why i was consulting the discord. Perhaps i need to contact them
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
GitHub
nicegui/examples/fastapi/frontend.py at main · zauberzeug/nicegui
Create web-based user interfaces with Python. The nice way. - zauberzeug/nicegui
i will do some rework on this and post the results here..need to add fastapi then separately
sounds good
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..
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
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 moresounds good
Hi got it running
Solution:
You need to declare fast api in the project e.g. in main.py
After this you can just add the below dockerfile
would you mind enclosing these code blocks with triple backticks?
@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.
it definitely will, thanks for coming back to report the final solution!