pythonpython-3.xflaskjinja2

Flask with pywebview cannot find templates


I'm running a properly working Flask website.

Templates are in the proper location /templates

When adding python webview (https://pywebview.flowrl.com/), like:

app = Flask(__name__)
webview.create_window('Some title', app)

it runs perfectly from PyCharm IDE. But after:

pyinstaller -F -w --icon=cpnits-picto.ico --name Butterfly main.py

the standalone cannot find the templates. Standalone runs fine, does all it should do, also gives output when not using templates (for testing purposes). But it does not find the templates.

I've tried things like:

app = Flask(__name__, template_folder='templates')
app = Flask(__name__, static_folder='./static', template_folder='./templates') 

and adding all sorts of paths to it. No success.

What am I doing wrong?

The complete code of a minimized test project that gives the same error:

from flask import Flask, render_template
import webview

app = Flask(__name__)

@app.get('/')
def index():
    return render_template('index.html')

@app.errorhandler(Exception)
def handle_error(e):
    return f'<h1>Error</h1><p>{e}</p>', 500

webview.create_window('Doei', app)
webview.start()
# pyinstaller -F -w --name TWV main.py

Where the template index.html is in the dir templates.

Thanks to the handle_error(e) the name of the error appears in the webviewer.


Solution

  • This problem is very common while we use the PyInstaller along with Flask. You will need to include templates and static folder while you are packing application.

    Note for Windows you might need to semicolon

    pyinstaller -F -w --add-data "templates:templates" --name Butterfly main.py 
    

    Next simply specify the templates in .spec file using datas parameter.