Is there an easy way to distribute a Flask server as an executable?

时光怂恿深爱的人放手 提交于 2021-01-27 07:10:40

问题


I'm building some simple editors with Backbone.js, and I'm hoping to be able to distribute them as apps for users to edit content in a mostly client-side way (i.e., I don't want users to have to futz with setting up stuff like MySQL or Apache).

So I was imagining a scenario like:

  1. User downloads a .zip file
  2. In the resulting opened folder, the user clicks index.html
  3. That opens in a browser
  4. Backbone app starts, stores data in localStorage
  5. The user can then export to CSV.

Believe it or not, that would solve my problem: I want to help users edit data in a browser and then get it back out in a familiar format (CSV can be loaded into Excel, for instance).

And I’d like to do this without forcing them to configure a server. It seems like this is almost possible in the HTML5 stack. However, in at least one browser (Chrome), this doesn't work, because I get errors like this one:

XMLHttpRequest cannot load file:///users/me/project/data/Appdata.json. Origin null is not allowed by Access-Control-Allow-Origin.

(Oddly enough, I don't get that error in Firefox, and the .js or .json files load fine.)

So at this point it seems to me that there's no way around having these users use something kind of local server to serve up the Backbone interface.

So, I'm trying to figure out how to build a distributable, cross-platform executable that will allow my users to start a Flask server. (I hope to build a REST backend to a Backbone.js app.)

Is this wishful thinking? I'm assuming I can get the people in question to install Python.

Is this doable? There seem to be many ways to package up Python programs, (pyinstaller? py2exe? ...) So I thought I would ask here in case someone might know of a solution for the stack I have in mind.

TIA!


回答1:


You can use Anthony Gordon McMillan’s Pyinstaller or Tuininga’s cx_Freeze

Quoting the PyInstaller website:

Features

Packaging of Python programs into standard executables, that work on computers without Python installed.

Multiplatform: works under

  • Windows (32-bit and 64-bit),
  • Linux (32-bit and 64-bit),
  • Mac OS X (32-bit only, 64-bit in git, see Features/MacOsCompatibility) and experimentally Solaris and AIX (in git).

Multiversion: works under any version of Python from 2.2 up to 2.7.




回答2:


My suggestion would be to create a thin service wrapper around your code. This will allow the server to run independently of your main codebase - also allowing the user to shut down the server directly (simply right clicking the service icon and selecting "Exit").

This SO answer should help you get started.

After reading your updated question, I think something like mongoose might be more suited to your task. It is an embeddable web server that is FLOSS and has python bindings. Flask might be overkill.




回答3:


Not easily. On Windows, you'd have to include Python itself. Mac and Linux usually have Python installed, but you can't be sure of what version so it's often easier to bundle your specific Python for them as well. Then you'd have to include all the dependencies that you want to run with in your package or be able to install them with pip, easy_install, etc.

You can use py2app and py2exe. This won't be cross-platform as you'll still need to make a different version for each target OS. The only way to make it cross-platform is to bundle all versions and have some cross-platform code execute the appropriate version for that platform.

If you need databases like MySQL or even SQLite things get more complicated as you'll have to include those too.



来源:https://stackoverflow.com/questions/10202711/is-there-an-easy-way-to-distribute-a-flask-server-as-an-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!