I am developing a number of python application for my internal users. We have a framework developed using a c# and wpf which every user have on their system. Now for the users to access the python application from that framework one option was to ask each users to install python in there system which is not possible. After some research in Google I came across the python freezer libraries
I was able to bundle or freeze a tensor flow , django Matlab application and host the same in our framework without asking user to install python by using pyinstaller. But for flask application I cannot find a proper freezer.
Is there any freezer for flask application. My requirements is to create a standalone exe for my flask application . I have both python 3 and python 2 in my build sever.
The approach described below is not using freezer and is suitable for python3 and working on Mac and Linux
In order to use python2 modify the shebang and pip tool to use python2
Significant Portions of the information below (beside the flask example and dependencies … ) are mentioned in this blog post Bundling Python files into a stand-alone executable
Additional information related to zip application are explained in The Python Zip Application Archive Format
Lets assume the flask application is as follow :
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
@app.route('/')
def root():
static_page='''
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
'''
return static_page
@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success',name = user))
else:
user = request.args.get('nm')
return redirect(url_for('success',name = user))
def main():
app.run()
It will be named moosh1.py.
Now constructing the following folder hierarchy:
app/
| __main__.py
| moosh/
| __init__.py
| moosh1.py
Where __main__.py is as follow:
import sys
import moosh
if __name__ == '__main__':
print("hello moosh1 - running on python %s.%s" %
(sys.version_info[0], sys.version_info[1]))
moosh.main()
and moosh/__init__.py is as follow:
from .moosh1 import main
__all__ = ['main']
For portability purposes install the flask dependencies into app folder
$ cd app
$ pip3 install flask --target ./
$ cd ..
Now zip it
$ cd app
$ zip -r ../app.zip *
$ cd ..
And now add shebang and make our stand-alone app executable
$ echo '#!/usr/bin/env python3' | cat - app.zip > app
$ chmod +x app
And there it is, app is a stand-alone python program with all the relevant dependencies
More elaborate programs will require more tweaking and may not work with this approach out of the box.
The largest program i found using a scheme like this is youtube-dl What is this binary file? Where has the code gone?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With