Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using cx_freeze on flask app

I am using Flask to develop a python app. At the moment, I want this app to be run locally. It runs locally fine through python, but when I use cx_freeze to turn it into an exe for Windows, I can no longer use the Flask.render_template() method. The moment I try to execute a render_template, I get an http 500 error, exactly as if the html template I'm trying to render does not exist.

The main python file is called index.py. At first I tried to run: cxfreeze index.py. This did not include the "templates" directory from the Flask project in the cxfreeze "dist" directory. So then I tried using this setup.py script and running python setup.py build. This now includes the templates folder and the index.html template, but I still get the http: 500 error when it tries to render the template.

from cx_Freeze import setup,Executable

includefiles = [ 'templates\index.html']
includes = []
excludes = ['Tkinter']

setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = '[email protected]',
options = {'build_exe': {'excludes':excludes,'include_files':includefiles}}, 
executables = [Executable('index.py')]
)

Here is an example method from the script:

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

If I run index.py then in the console I get:

 * Running on http://0.0.0.0:5000/
 rendering index
 127.0.0.1 - - [26/Dec/2012 15:26:41] "GET / HTTP/1.1" 200 -
 127.0.0.1 - - [26/Dec/2012 15:26:42] "GET /favicon.ico HTTP/1.1" 404 -

and the page is displayed correctly in my browser, but if I run index.exe, I get

 * Running on http://0.0.0.0:5000/
rendering index
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET /favicon.ico HTTP/1.1" 404 -

and

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

in my browser.

If I return raw html, e.g.

@app.route('/index', methods=['GET'])
def index():
    print "rendering index"
    return "This works"

then it works fine. So a possible work around is to stop using Flask's templates and hardcode all the html logic into the main python file. This gets very messy though, so I'd like to avoid it if possible.

I'm using Python 2.7 32-bit, Cx_freeze for Python 2.7 32-bit, and Flask 0.9

Thanks for any help and ideas!

like image 450
Sixhobbits Avatar asked Dec 26 '12 13:12

Sixhobbits


People also ask

Can I use Uvicorn with Flask?

Gunicorn with Uvicorn Workers Gunicorn is mainly an application server using the WSGI standard. That means that Gunicorn can serve applications like Flask and Django.

Can we convert Flask app to exe?

Hey coder, The answer is yes, python files can be converted to executables.

How do I make my Flask app accessible?

Today, there are two ways to expose your flask application to the internet. Deploy the web application in your office server which has a public IP address and domain name. Deploy the web application in the cloud such as AWS, MS Azure, GCP or web hosting companies like GoDaddy, SiteGround, A2Hosting etc.


1 Answers

After many false trails trawling through the Flask and Jinga modules, I finally found the problem.

CXFreeze does not recognize that jinja2.ext is a dependency, and was not including it.

I fixed this by including import jinja2.ext in one of the python files.

CXFreeze then added ext.pyc to library.zip\jinja. (Copying it in manually after the build also works)

Just in case anyone else is mad enough to try use Flask to develop locally run apps :)

like image 86
Sixhobbits Avatar answered Oct 30 '22 17:10

Sixhobbits