DEV Community

artydev
artydev

Posted on

Desktop APPS with Flask an PyWebview

Here is a recipe on how to accomplish this :

Create a folder sructure like this one :

Image description

In the app.py file :


from flask import Flask, render_template
import webview
import sys
import os

base_dir = '.'
if hasattr(sys, '_MEIPASS'):
    base_dir = sys._MEIPASS

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

tf = resource_path('templates')
sf = resource_path('static')

app = Flask(__name__, static_folder=sf, template_folder=tf)

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

if __name__ == '__main__':
    webview.create_window('My Flask App', app)
    webview.start()
Enter fullscreen mode Exit fullscreen mode

In the main.spec file :

# -*- mode: python ; coding: utf-8 -*-


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[
        ('static', 'static'),
        ('templates', 'templates'),
    ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
Enter fullscreen mode Exit fullscreen mode

And finally, run the following command:

pyinstaller main.spec
Enter fullscreen mode Exit fullscreen mode

The exe is in the dist folder

Image description

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more