Suggest a standalone python web framework? - python

I have a python program that I would like to present as a simple web application. The program currently uses sqlite for storage. I also need to distribute the whole thing to colleagues so having something standalone and easy to start would be ideal ( no install if possible). This web app is meant to be used locally , not by multiple users over a network.
Is there a suitable python framework that might fit my needs? I looked at Django so far but it seems a bit heavy handed for what I need.
Thanks for any suggestions.

I have never tried it myself, but you could try Bottle:
Bottle is a fast, simple and lightweight WSGI micro web-framework for
Python. It is distributed as a single file module and has no
dependencies other than the Python Standard Library.

try http://docs.python.org/library/simplehttpserver.html

As web frameworks are not part of the standard lib, you will have to install something in every case. I would propse to look at http://flask.pocoo.org/. It has a build in WSGI server.

Lots of choices for Python web frameworks! Another is web2py which is designed to work out of the box and allows, but doesn't require, through-the-web development. It is mature and has a strong community and is well-documented.

Tornado as a framework may be a lot more than what you're looking for. However it will meet the requirement of being a completely python based web server. http://tornadoweb.org
I generally just download the source, drop it in /tornado/ of my project and do includes there from the app.

I don't think that any web framework is specifically oriented for the use case you're talking about; They all assume they are running on a server and there's a browser on a remote machine that is accessing them.
A better approach is to think about the HTTP server you'll be using. It's probably preferable to use a server that's as easy to pack and ship as the rest of the python code you'll be using. Now most frameworks provide a 'development' server that's easy to invoke from the command line, but most of them are intended to be "easy for the developer" which often means they are restricted to a single thread. This is bad for deployment because single threaded servers will always feel a bit sluggish.
CherryPy stands out in contrast, by providing a full featured, embedded server that's easy to configure for many use cases, and is available by default with the rest of the framework. There are probably others, but I haven't used 'em.

Related

Run a Python 2.7 Script on a Webpage

I have a couple of Python 2.7 scripts I need to have them run from a webpage. I've never done this before, although I'm comfortable coding in Python and well versed in Linux, Apache, Nginx.
I see many different frameworks like the old CGI, WSGI, Django, etc.
I need some recommendations for a framework that would require not much changes on my Python code for it to run on a webpage.
It seems CGI would be fairly easy for me, but since there are many newer frameworks, I need some advices.
Thanks!
Flask is probably the most straightforward Python web framework to use. It touts itself as a micro framework, and is very, very fast to get up and running with.
If you have a relatively straightforward set of requirements, I'd highly recommend giving it a look over. You can find the "hello" world example on the project's homepage here: http://flask.pocoo.org/
Depending on your project's requirements (does it need to be highly available/resilient?), you may even be able to get by using Flask's built-in development webserver along with a process control system such as supervisord. I wouldn't recommend this for heavy or production workloads, but for something running out of production this set up would work just fine for you.
If you're looking for client side browser frameworks in Python, there are skulpt and brython.
When I was looking for a similar framework, I decided to learn leave python for a javascript framework (particularly AngularJS and Node JS) for a client side language. I personally felt that the size of community and learning resources available in those two areas were more robust, and provided greater opportunity to master the framework.

How to expose an NLTK based ML(machine learning) Python Script as a Web Service?

Let me explain what I'm trying to achieve. In the past while working on Java platform, I used to write Java codes(say, to push or pull data from MySQL database etc.) then create a war file which essentially bundles all the class files, supporting files etc and put it under a servlet container like Tomcat and this becomes a web service and can be invoked from any platform.
In my current scenario, I've majority of work being done in Java, however the Natural Language Processing(NLP)/Machine Learning(ML) part is being done in Python using the NLTK, Scipy, Numpy etc libraries. I'm trying to use the services of this Python engine in existing Java code. Integrating the Python code to Java through something like Jython is not that straight-forward(as Jython does not support calling any python module which has C based extensions, as far as I know), So I thought the next option would be to make it a web service, similar to what I had done with Java web services in the past. Now comes the actual crux of the question, how do I run the ML engine as a web service and call the same from any platform, in my current scenario this happens to be Java. I tried looking in the web, for various options to achieve this and found things like CherryPy, Werkzeug etc but not able to find the right approach or any sample code or anything that shows how to invoke a NLTK-Python script and serve the result through web, and eventually replicating the functionality Java web service provides. In the Python-NLTK code, the ML engine does a data-training on a large corpus(this takes 3-4 minutes) and we don't want the Python code to go through this step every time a method is invoked. If I make it a web service, the data-training will happen only once, when the service starts and then the service is ready to be invoked and use the already trained engine.
Now coming back to the problem, I'm pretty new to this web service things in Python and would appreciate any pointers on how to achieve this .Also, any pointers on achieving the goal of calling NLTK based python scripts from Java, without using web services approach and which can deployed on production servers to give good performance would also be helpful and appreciable. Thanks in advance.
Just for a note, I'm currently running all my code on a Linux machine with Python 2.6, JDK 1.6 installed on it.
One method is to build an XML-RPC server, but you may wish to fork a new process for each connection to prevent the server from seizing up. I have written a detailed tutorial on how to go about this: https://speakerdeck.com/timclicks/case-studies-of-python-in-parallel?slide=68.
NLTK based system tends to be slow at response per request, but good throughput can be achieved given enough RAM.

My first web app (Python): use CGI, or a framework like Django?

I don’t want to burden you all with the details, but basically I’m a 2nd year compsci student with no Web dev experience.
Basically I want to create a small “web app” that takes in input from a html form, have a python script perform some calculations, and re-display those results in your browser.
As of right now, I have the form and script built. However, when I try to test the form, instead of running the script, my browser tries to download it. To my understanding, this is a cgi script problem, and that I must create a web server in order to test this script.
And heres were I’m stuck. I know little to nothing about web servers and how to set them up. On top of that I’ve heard that GCI scripting is a thing of the past, and requires major overhead in order to run properly.
This leads to my questions. How do I go about completing my app and testing my cgi script? Do I install apache and mess around with it or should I be looking into something like google app engine? Are there other ways to complete this task without cgi scripts? Where do frameworks like Django fit into this?
Django, while being nice, all-encompassing and well-supported, is sometimes too much for a small web application. Django wants you to play by its rules from the beginning, you'll have to avoid things like the database and admin panels if you don't need them. It's also easier, with Django, to follow its project layout, even when it's too complex for a simple app.
The so-called micro frameworks might suit you better for your small app. They are built upon the opposite principle: use the bare minimum of features now, add more as you need them.
Flask is based on Werkzeug WSGI library and Jinja2 templating (the latter is switchable), is extensively documented (with notes concerning virtualenv and stuff) and well-suited for small and larger apps alike. It comes bundled with an auto-reloading dev server (no need for Apache on your dev machine) and Werkzeug-powered interactive debugger. There are extensions for things like HTML forms and database ORM.
Bottle is as small as a microframework can get, consisting of 1 (one) file, dev server included. Drop it into your project folder and start hacking. The built-in SimpleTemplate templating engine is switchable, but the dev server is flakier in comparison to Flask's. The documentation is less complete, and, in my opinion, the whole thing is less polished and convenient as Flask.
In both cases, you use dev server locally, and then deploy using WSGI, the server interface for Python web apps which both frameworks support. There are many ways to deploy a WSGI app, Apache mod_wsgi being one of the popular ones.
I'd totally go with Flask unless one dependency (Bottle) is better than three (Flask, Jinja2 and Werkzeug).
(There are many other frameworks as well, so wait for their users to come and tell about them. I'd suggest to avoid web.py: it works, but is full of magic, and is inelegant compared to Flask or Bottle.)
One way of getting to working webapp quickly is by first understanding, and then modifying, something like the App Engine "guestbook" example. This has the benefit that much of the otherwise necessary tedium of running a web server and setting up a database server (assuming you need persistence) is done for you. App Engine also provides a fairly flexible development environment. It's certainly not the only way to go, and I'll admit to bias in recommending it, but it's fairly low friction.
GCI scripting is hardly a thing of the past, though it's not what the cool kids are doing. CGI has the benefit, and the curse, of exposing more of the raw plumbing. It forces you to understand a lot about primitive (in the low-level sense) web architecture, but it's also a bit of a large bite to chew on if you have an immediate problem to solve that can solved by simpler means.
It appears most python web development seems to be done by frameworks these days. There are a couple reasons for this:
a plethora of mature tools. Django has built in user auth, built in database management, built in sessions, built in just about everything ORM which lets you seamlessly supports a couple databases.
Built in webservers. The larger python frameworks like django and pylons have built in webservers. Django has a very simple webserver python manage.py startserver (that simple) That makes it extremely easy to create and debug applications. It is single threaded so dropping a debugger into it is painless
Huge communities. If you have a django question it will be answered very quickly the so community is huge.
The django tutorial will introduce you to all the major aspects of development. It is only 4 pages and you will be able to get your app going a lot simpler than having to read, learn and fiddle with an apache setup.
https://docs.djangoproject.com/en/dev/intro/tutorial01/
Although django for right now might be overkill if your app is just going to be 1 form and a script to process it. Because of its seamless testing framework it is quite easy to grow any project. I have never used flask or bottle or the other microframeworks, but I would keep in mind where your project will be in the future.
As for where django fits into this, it is a full stack framework encompassing presentation (templates), data management (server orm), authentication, middleware, forms ... everything necessary to create a completely inclusive web application. Django and almost all other python frameworks implement the wsgi standard. It is an interface that allows for interoperation between webservers. http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface it is pretty dry and you will never have to interface it directly. That is what these frameworks do under the hood.
Why setup and maintain your own webserver if you can use app engine. It has an excellent SDK for testing your code. Here is an example https://developers.google.com/appengine/docs/python/gettingstarted/handlingforms
And Django you will find here : https://developers.google.com/appengine/docs/python/gettingstarted/templates
I prefer to use Jinja for templating.
Django comes with its own server, but in your case i would recommend apache and mod_python since it seems to be a rather simple site you're building.
Setting up Apache is a breeze and a simple search on the web should give you all you need.
You can find more information on mod_python here read up a little bit on it and then google after a tutorial that fits your needs.

is apache necessary while we have python's built-in SimpleHttpServer

I am new to the python web dev world and kind of confused about why we need a apache enviroment while we could run python web app with its build-in http sever? Also, from my experience, I could run an django app without setting up anything else. then why we still need apache + mod_wsgi? for performance?
actually what really confuse me is.... how my entry point of code should be written? e.g. I heard there are other advanced 'web server' as well like cherrypy/Tornado and every single of them will require different entry-point code. so I wonder if apache(+ mod_wsgi) is not overlapping with other web framework(I called them web server above)? (in most case) we should be using apache on production but use others as an 'addon'? thanks
Performance, stability, scalability, security, ...
The built in HTTP server is useful for simple testing or quickly running a web app on your development machine, but is in no way as scalable as the Apache server. Security will also likely be less hardened on the built-in one.
Also, Apache allows you to handle many extra things, such as vhosts, multiple kinds of server-side platforms (for instance, a Ruby on Rails app and a Django one on the same port/IP), which are harder to achieve with the built-in server.
Apache is way better than the python SimpleHTTPServer.
For one thing the SimpleHttpServer is single threaded, but apache can easily handle multiple threads. Apache can also be configured in many ways that SimpleHttpServer cannot do. Apache has an easy to use logging of requests, which is helpful for debugging & logging.
These days the standard entry point is a WSGI application object. Pretty well everything supports it. How each web framework exposes one, and how you set up each WSGI hosting mechanism to then use it is different. At the core though, the actual interface between web server and application is the same.
Performance and scalability would be the reasons to go with Apache in production. SimpleHTTPServer is fine for testing and internal use though.
Yes, in general you need those for performance.
If you want to avoid the complexity of setting up Apache until you really have to (which could be reasonable if you're short on time and/or lack experience) you will probably be better off by using CherryPy to serve Django. It has an all-Python web server with much better performance than the built-in.
You can find instructions for that here.

How to develop a simple web application with server-side Python

I am wondering how to go about implementing a web application with Python.
For example, the html pages would link to python code that would give it increased functionality and allow it to write to a database.
Kind of like how Reddit does it.
If you're looking for server side programming with databases and html templates etc, I think Django is great, along with Pyramid. However, I use Flask ( http://flask.pocoo.org/ ) for this since it is easy to use, learn and deploy even though it may not have as much support as the before mentioned 2 framework since it's just a microframework, using the Jinja2 templating engine, including a development test server with it's own debugger.
On the other hand, if you're going for client-side programming (i.e. in browser implementation ) You can look up .NET Ironpython or even Brython which uses python like javascript.
You might want to check out mod_wsgi or mod_python.
What Is mod_wsgi?
The aim of mod_wsgi is to implement a simple to use
Apache module which can host any Python application which supports the
Python WSGI interface. The module would be suitable for use in hosting
high performance production web sites, as well as your average self
managed personal sites running on web hosting services.
-
Current State of Mod_Python
Currently mod_python is not under active development. This does not
mean that it is "dead" as some people have claimed. It smiply means
that the code and the project are mature enough when very little is
required to maintain it.
This is a good article from the Python website:
http://docs.python.org/howto/webservers.html
Plain CGI is a good starting point to learn about server side scripting, but it is an outdated technology and gets difficult to maintain after certain level of complexity. I would think it is no longer used in industrial-grade web server anymore. Plus you have to setup a web server and then install some module to interpret python script (like Apache with mod_python) just to get started.
I had some experience with Django (https://www.djangoproject.com/) and found it fairly easy to get started with since they come with development test server. All you need to have is a Python interpreter + Django and you can get up-and-running quickly and worry about the deployment setup later. They have pretty good documentation for beginner as well.
We have never used Python for a web site without a framework. In our case that is Django. In other words, we do not use Python for our web sites the way Perl can be used, just having Apache run a Perl script.
The recommendations you have received about Django are sound. If you go the Django route, Graham Dumpleton and the modwsgi Google group were very helpful to me. I could
not have gotten mod_wsgi deployed on Red Hat Enterprise 5 64-bit without Graham's help.
Whether you choose Django or "straight" Python, you will need to become familiar with mod_wsgi.
Good luck in quantum time, which means by now, I hope this all worked out for you.

Categories