localhost server in compiled Python program - python

Background:
I am still new to Python so apologies if this is a naive question. I was originally looking to develop a tool for Windows with a GUI, however, reading around I get the impression that it may be simpler to have my compiled python code somehow 'wrapped' in simpleHTTPServer so users can interact via localhost (much like Jupyter notebook/labs).
Question:
Is there a standard/recommended way to have a compiled python program interface with the user via a localhost port in their web browser? I assume this is a fairly common approach and that a package may already even exist but I've not been able to find anything that gives guidance on how to tackle it.
Alternatively, is is feasible to somehow compile a Django development project to run on localhost?
Ideally, I want an executable file that users can run and their browser opens up at the localhost port required.

If you wish to quickly make a GUI based Python tool, you should consider using Django, a very straightforward framework for WebApps. It is very quick to learn and implement, and you shall very easily be able to integrate highly advanced functionalities too into your tool with ease.
When it comes to distribution of code, you can convert the entire Django App into a windows exe file for ease of distribution and use. There are many python packages that allow you to port python to windows executable files. I would recommend using PyInstaller. You may find this link useful: Recipe Executable From Django.
Alternatively, as someone suggested, you may use Docker, but that would be heavier in terms of space utilized, and will require the user to install the same.
Hope this helps!

Related

Install-less python web app on headless CentOS 5.x-6.x

I have a Linux web application which installs a webserver, a database, digital certificates etc using a set of .sh scripts.
In order to simplify user interaction during installation such as entering passwords, certificate details and such, I want to create a GUI installer. After much deliberation, following are some decisions and related questions
The target systems may or may not have a Desktop or a monitor installed. So providing a web interface to the install process may be the way to go. User would copy the application to the target machine, start the webservice which would then expose a web interface to continue the install. Would Python be a good choice for this?
Since this is an installer itself, the requirements to run it must be practically nil. This requires
Use python's built in SimpleHTTPServer. This will be used the one time during installation and then be killed. Any caveats to using the default python web server?
Compile app into standalone binary using one of the Freezing utilities. We don't want to depend on the user having python on their system and have been asked to account for admins who've removed python due to whatever reason. Is this precaution necessary?
Any comments on the general approach or alternative options will be greatly appreciated.

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

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:
User downloads a .zip file
In the resulting opened folder, the user clicks index.html
That opens in a browser
Backbone app starts, stores data in localStorage
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!
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.
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.
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.

Start creating websites by using Python

I am basically a PHP guy. now moving towards python. I am starting to learn python.
How do I install it and start working it, and develop websites .
I got totally confused with the alternative implementations in the download section of the Python site. Can you tell me what "alternative implementations" means?.
I mean to say: I can create a .php file in my server and then access it from browser like http://example.com/index.php, so I was wondering whether I can do the same with python, like creating a .py file and accessing from browser http://example.com/index.py.
Just as a disclaimer, I interpret you saying "run Python in a browser" as "making a website with Python."
If you want to start writing web applications in Python, you can either use CGI or use one of its many web app frameworks. Python is not like PHP in the sense that you can't just embed it in HTML. Many of those frameworks come with development servers that you can use to test your web app (by looking at it in a browser).
A particularly good Python web framework is Django.
I really do recommend that you do the Python tutorial before you dive into any of those frameworks, though. Python is not only for writing web applications, so you'll have to get some fundamentals down first before any of that makes sense to you.
As for installing Python, I recommend you take the version you got with your OS if you use Mac or Linux, or installing the Python 2.7.1 32-bit binary from python.org if you use Windows. The alternative implementations include a Python that runs on the Java virtual machine and one that runs on the .NET Common Language Runtime, but for your purposes the reference implementation, CPython, should work fine.
Python by default is a general purpose scripting language and is not meant specifically for web application development (like PHP is primarily). So, you'll first need to download and install Python (choose the version that's appropriate for your OS) from www.python.org I'd recommend v2.7.1 but you can try out v3.2 if you'd like.
The other versions (IronPython, Jython, etc) are Python implementations on other platforms (.NET and JVM respectively), and in all likelihood you don't need to bother with them unless you really want to.
To start working with Python and create web applications you will also need to download and install a Python-based web framework. There are many of them, too many in fact to list here. However, there is a page on the Python Wiki that has a list of useful frameworks for web development.
You don't "run it from a browser". You don't run PHP from a browser, either.
The way to use Python for web development is to use a framework like Django or Pyramid.
Sage or CodeNode let you run Python in a browser.
You can get a limited Python experience by pointing your browser to http://shell.appspot.com/ . You will be running Python 2.5.2 on Google's site.

Where can I find a browser based editor for python that works with IE7?

I'd like to get some code editing for a free software project done during some free time that I will have today at my university. There's just one small problem: We're almost entirely a windows environment (mostly thin clients at that) and the only source code editor installed is jGRASP.
So, I need a source code editor which is usable without installing anything (something browser, or Java Web Start based). I would use Notepad++, but I think downloading and running some unapproved exe may be in violation of our acceptable use policy, and it isn't that efficient of an editor for python anyway.
Any suggestions are appreciated.
The best webbased editor I know is Ace.
If you can't execute downloaded exes, this maybe mute, but SciTe is reasonably useable and does not require you to install anything - just download and run.
Putty and vim would be my solution (Requires a remote host running 'nix)
Putty is obviously a 3rd party binary, but a fairly common one.
I'm sure there are web based ssh clients to be had.

What are some successful methods for deploying a Django application on the desktop?

I have a Django application that I would like to deploy to the desktop. I have read a little on this and see that one way is to use freeze. I have used this with varying success in the past for Python applications, but am not convinced it is the best approach for a Django application.
My questions are: what are some successful methods you have used for deploying Django applications? Is there a de facto standard method? Have you hit any dead ends? I need a cross platform solution.
I did this a couple years ago for a Django app running as a local daemon. It was launched by Twisted and wrapped by py2app for Mac and py2exe for Windows. There was both a browser as well as an Air front-end hitting it. It worked pretty well for the most part but I didn't get to deploy it out in the wild because the larger project got postponed. It's been a while and I'm a bit rusty on the details, but here are a few tips:
IIRC, the most problematic thing was Python loading C extensions. I had an Intel assembler module written with C "asm" commands that I needed to load to get low-level system data. That took a while to get working across both platforms. If you can, try to avoid C extensions.
You'll definitely need an installer. Most likely the app will end up running in the background, so you'll need to mark it as a Windows service, Unix daemon, or Mac launchd application.
In your installer you'll want to provide a way to locate a free local TCP port. You may have to write a little stub routine that the installer runs or use the installer's built-in scripting facility to find a port that hasn't been taken and save it to a config file. You then load the config file inside your settings.py and whatever front-end you're going to deploy. That's the shared port. Or you could just pick a random number and hope no other service on the desktop steps on your toes :-)
If your front-end and back-end are separate apps then you'll need to design an API for them to talk to each other. Make sure you provide a flag to return the data in both raw and human-readable form. It really helps in debugging.
If you want Django to be able to send notifications to the user, you'll want to integrate with something like Growl or get Python for Windows extensions so you can bring up toaster pop-up notifications.
You'll probably want to stick with SQLite for database in which case you'll want to make sure you use semaphores to tackle multiple requests vying for the database (or any other shared resource). If your app is accessed via a browser users can have multiple windows open and hit the app at the same time. If using a custom front-end (native, Air, etc...) then you can control how many instances are running at a given time so it won't be as much of an issue.
You'll also want some sort of access to local system logging facilities since the app will be running in the background and make sure you trap all your exceptions and route it into the syslog. A big hassle was debugging Windows service startup issues. It would have been impossible without system logging.
Be careful about hardcoded paths if you want to stay cross-platform. You may have to rely on the installer to write a config file entry with the actual installation path which you'll have to load up at startup.
Test actual deployment especially across a variety of firewalls. Some of the desktop firewalls get pretty aggressive about blocking access to network services that accept incoming requests.
That's all I can think of. Hope it helps.
If you want a good solution, you should give up on making it cross platform. Your code should all be portable, but your deployment - almost by definition - needs to be platform-specific.
I would recommend using py2exe on Windows, py2app on MacOS X, and building deb packages for Ubuntu with a .desktop file in the right place in the package for an entry to show up in the user's menu. Unfortunately for the last option there's no convenient 'py2deb' or 'py2xdg', but it's pretty easy to make the relevant text file by hand.
And of course, I'd recommend bundling in Twisted as your web server for making the application easily self-contained :).

Categories