DEV Community

loading...

Use FastAPI to create a desktop application (no Electronjs)

Alin Climente
Independent contractor interested in web development and business automation. Contact me on Linkedin.
Updated on ・1 min read

Not only JS folks can do it we can do it too!

First, install this library:

pip install flaskwebgui
Enter fullscreen mode Exit fullscreen mode

Next add the following code in a main.py file:

#main.py

from fastapi import FastAPI
from flaskwebgui import FlaskUI # import FlaskUI

app = FastAPI()
ui = FlaskUI(app) # feed app and parameters

@app.get("/")
def read_root():
    return {"message": "Works with FastAPI!"}

if __name__ == "__main__":
    ui.run()

Enter fullscreen mode Exit fullscreen mode

Alternatively, next to main.py create a file called gui.py and add the following contents:

#gui.py

from flaskwebgui import FlaskUI
from main import app

FlaskUI(app, width=600, height=500).run()
Enter fullscreen mode Exit fullscreen mode

Next start the application with:

python main.py 
#or
python gui.py #in case you created gui.py 
Enter fullscreen mode Exit fullscreen mode

Fastapi will be served by uvicorn.

And that's all!

Discussion (0)

Forem Open with the Forem app