DEV Community

Alin Climente
Alin Climente

Posted on • Edited on

Use FastAPI to create a desktop application (no Electronjs)

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!

Top comments (1)

Collapse
 
abulka profile image
Andy Bulka

I'm getting the error AttributeError: 'FastAPI' object has no attribute 'after_request' whether I run python main.py or python gui.py.

I have flaskwebgui, flask and fastapi installed ok.

Had to change the startup in both files to FlaskUI(app, start_server='fastapi', width=600, height=500).run()