4

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!

  • 2
    This is hearsay, but if you go down this path, you might have an easier time with Bottle than Flask when it comes to packaging and distribution. – dumbmatter Apr 18 '12 at 16:59

3 Answers3

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.

Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Hi burhan, thanks for your comment. I'll clarified my comment a bit to explain exactly what I'm after. I'm looking for a way to distribute a single-machine _server_ that my users can start up to persist data to a database locally. –  Apr 18 '12 at 04:28
  • For all practical purposes yes. –  Apr 18 '12 at 04:40
2

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.

Elvis D'Souza
  • 2,273
  • 1
  • 23
  • 31
1

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.

six8
  • 2,886
  • 21
  • 20
  • 1
    Hi Cixate. What if they do have Python installed locally? I know most of the people in question—they're not totally clueless technically, so things like getting Python installed on Windows are probably doable. And it will be a small user group. So shouldn't it be possible to somehow double click a Flask (or whatever) server and then have that do something like: import webbrowser; import flask; flask.startserver() # or whatever; webbrowser.open('http://localhost:8000/myapp.html') –  Apr 18 '12 at 04:36
  • Hmm I just tried that last option and there doesn't seem to be a way to make a Python script actual run if you double click the icon, at least under OSX... sigh, I'm not sure if I'm even making any sense. –  Apr 18 '12 at 04:41
  • Looks like http://www.pyinstaller.org/ (@Elvis's answer) is a more viable option. I wasn't aware of it. – six8 Apr 18 '12 at 04:42