DEV Community

Alin Climente
Alin Climente

Posted on • Edited on

5 2

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!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (2)

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

Collapse
 
lexus1083 profile image
Алексей Сукач

Открывается браузер с надписью: не удается открыть эту страницу. И все.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay