Skip to main content

Create desktop applications with Flask/Django/FastAPI!

Project description

Flaskwebgui

Downloads PyPI

Create desktop applications with Flask/FastAPI/Django!

Install

pip install flaskwebgui

If you are using conda checkout this link.

Usage with Flask

Let's say we have the following flask application:

#main.py

from flask import Flask  
from flask import render_template
from flaskwebgui import FlaskUI # import FlaskUI

app = Flask(__name__)


@app.route("/")
def hello():  
    return render_template('index.html')

@app.route("/home", methods=['GET'])
def home(): 
    return render_template('some_page.html')


if __name__ == "__main__":
  # If you are debugging you can do that in the browser:
  # app.run()
  # If you want to view the flaskwebgui window:
  FlaskUI(app=app, server="flask").run()
   

Install waitress for more performance.

Usage with Flask-SocketIO

Let's say we have the following SocketIO application:

#main.py
from flask import Flask, render_template
from flask_socketio import SocketIO
from flaskwebgui import FlaskUI


app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route("/")
def hello():  
    return render_template('index.html')

@app.route("/home", methods=['GET'])
def home(): 
    return render_template('some_page.html')


if __name__ == '__main__':
    # socketio.run(app) for development
    FlaskUI(
        app=app,
        socketio=socketio,
        server="flask_socketio",
        width=800,
        height=600,
    ).run()

App will be served by flask_socketio.

Usage with FastAPI

Pretty much the same, below you have the main.py file:

#main.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI
from flaskwebgui import FlaskUI

app = FastAPI()

# Mounting default static files
app.mount("/dist", StaticFiles(directory="dist/"), name="dist")
app.mount("/css", StaticFiles(directory="dist/css"), name="css")
app.mount("/img", StaticFiles(directory="dist/img"), name="img")
app.mount("/js", StaticFiles(directory="dist/js"), name="js")
templates = Jinja2Templates(directory="dist")


@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@app.get("/home", response_class=HTMLResponse)
async def home(request: Request): 
    return templates.TemplateResponse("some_page.html", {"request": request})


if __name__ == "__main__":
    
    FlaskUI(app=app, server="fastapi").run()

FastApi will be served by uvicorn.

Usage with Django

Next to manage.py file create a gui.py file where you need to import application from project's wsgi.py file.

├── project_name
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── gui.py # this 
├── manage.py

In gui.py file add below code.

#gui.py
from flaskwebgui import FlaskUI
from djangodesktop.wsgi import application as app

if __name__ == "__main__":
    FlaskUI(app=app, server="django").run()

Next start the application with:

python gui.py  

Install waitress for more performance.

Configurations

Default FlaskUI class parameters:

  • server: Union[str, Callable[[Any], None]]: function which receives server_kwargs to start server (see examples folder);
  • server_kwargs: dict = None: kwargs which will be passed down to server function;
  • app: Any = None: wsgi or asgi app;
  • port: int = None: specify port if not a free port will set;
  • width: int = None: width of the window;
  • height: int = None: height of the window;
  • fullscreen: bool = True: start app in fullscreen (maximized);
  • on_startup: Callable = None: function to before starting the browser and webserver;
  • on_shutdown: Callable = None: function to after the browser and webserver shutdown;
  • browser_path: str = None: set path to chrome executable or let the defaults do that;
  • browser_command: List[str] = None: command line with starts chrome in app mode;
  • socketio: Any = None: socketio instance in case of flask_socketio;

Develop your app as you would normally do, add flaskwebgui at the end or for tests. flaskwebgui doesn't interfere with your way of doing an application it just helps converting it into a desktop app more easily with pyinstaller or pyvan.

Advanced Usage

You can plug in any python webframework you want just by providing a function to start the server in server FlaskUI parameter which will be feed server_kwargs.

Example:

# until here is the flask example from above

def start_flask(**server_kwargs):

    app = server_kwargs.pop("app", None)
    server_kwargs.pop("debug", None)

    try:
        import waitress

        waitress.serve(app, **server_kwargs)
    except:
        app.run(**server_kwargs)


if __name__ == "__main__":

    # Custom start flask

    def saybye():
        print("on_exit bye")

    FlaskUI(
        server=start_flask,
        server_kwargs={
            "app": app,
            "port": 3000,
            "threaded": True,
        },
        width=800,
        height=600,
        on_shutdown=saybye,
    ).run()

In this way any webframework can be plugged in and the webframework can be started in a more customized manner.

Checkout examples for more information.

Distribution

You can distribute it as a standalone desktop app with pyinstaller or pyvan.

Observations

  • Parameters width, height and maybe fullscreen may not work on Mac;
  • Window control is limited to width, height, fullscreen;
  • Remember the gui is still a browser - pressing F5 will refresh the page + other browser specific things (you can hack it with js though);
  • You don't need production level setup with gunicorn etc - you just have one user to serve;
  • If you want to debug it just run it as you would normally do with app.run(**etc), uvicorn.run(**etc), python manage.py runserver etc. flaskwebgui does not provide auto-reload you already have it in the webframework you are using;

Credits

It's a combination of https://github.com/Widdershin/flask-desktop and https://github.com/ChrisKnott/Eel

It has some advantages over flask-desktop because it doesn't use PyQt5, so you won't have any issues regarding licensing and over Eel because you don't need to learn any logic other than Flask/Django/FastAPI/etc.

Submit any questions/issues you have! Fell free to fork it and improve it!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flaskwebgui-1.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flaskwebgui-1.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file flaskwebgui-1.0.2.tar.gz.

File metadata

  • Download URL: flaskwebgui-1.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for flaskwebgui-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c557f0a988516070cbabb3e07047c102068e935cb844ab4162496fc77b24c50a
MD5 83f98f73fcfa777f1362d187342db718
BLAKE2b-256 744536d3a61f373ee07f93a4fe152e19365f7e2e64c239ab478c2416adca593c

See more details on using hashes here.

File details

Details for the file flaskwebgui-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: flaskwebgui-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for flaskwebgui-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e2b664ff88425b30560a170d8896017ed595a4b3fef53a8a54c3bbca6174ac7c
MD5 25e4b72da95e29abfb94716b92311360
BLAKE2b-256 74796be3af32e8bcad29425bd8c213d3d58b2e6484bb80628202cbbea40ecde3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page