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 can I execute a Python script on a webpage?
I've used XAMPP to create the Apache server. Are there any tutorials/examples or guides on how to execute a .py script? I'm using Windows 7 and have installed Python on my local machine. If I access the .py script via the web link, it looks as if its HTML code and nothing is executed.
Typically, you don't execute Python in the browser. Instead, the browser accesses a resource (or "webpage", like http://example.com/mypage) by requesting the resource from the server. The server (for example, Apache), when administered correctly, passes off handling of the request to some Python script. Then, your Python script creates some output (for example, HTML) which the server then returns to the browser for the browser to display.
However, some web sites have found it useful to have logic (scripts) run in the browser, rather than on the server. The standard way of doing this is using JavaScript (although in the past there WERE other languages built into browsers, such as VBScript in Internet Explorer).
Right now, pretty much all browsers have settled on JavaScript as THE scripting language in the browser. In order for you to use any other language in the browser (including Python), the browser must support that scripting language (or needs to have an add-on to support that scripting language). Simply having Python installed on your client alongside the browser is not enough. For more information, please see the Python documentation Web Browser Programming.
Another option is to use something like Pyjs. This is a library that has you write your code in Python, and converts the necessary parts to JavaScript. This isn't exactly "Python in the browser", but it might be something you are looking for.
I guess this links will help....
https://community.apachefriends.org/f/viewtopic.php?t=42975
Usually a good resource for Python is the official documentation.
They do a great job of explaining many aspects of Python. Using Python on the web is a big part and they have a great overview.
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.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Python noob here: On a Python enabled web server, how do I use Python?
I want give web development using Python a shot, but I'm failing hard. I don't even know if I CAN run Python. The webpage for my web host says it supports all kinds of stuff, including Python. I've never touched anything CGI related and this does not appear to be a noob friendly area.
Below is all the information I could think of that might be useful:
Shared host: Yes
Webhost: dibbsonhosting.com
Server OS: linux
Python installed: Yes (or so says the webhost)
mod_python installed: Don't Know
WSGI installed: Don't Know
Access to http.conf: No
Terminal access: No
Cpanel: Yes
FTP: Yes
Django: downloaded latest stable release, not uploaded anywhere, not
sure where to put it.
DB: MySQL 5.x
I really want to use Python for web development! Thanks!
Alright, I'm not going to hold your hand, but let me suggest:
Start off by installing and running it on your own computer, if at all possible. This lets you see what you can do with Python before you have to worry about the complexities of setting it up with a webserver.
If you've never used Python before, then before you start trying to use it for web development, take a few minutes getting the feel of it: what function definitions look like, the abilities of slicing, the neatest way to do loops, and so on. There are various good tutorials available: Dive into Python claims to be aimed at people with experience programming in other languages.
Start learning with a microframework, rather than Django. When you want to do serious websites, you can take advantage of the power of Django, but for now, keep it straightforward. As was suggested in your other question, Flask and Bottle are good contenders.
MALON,
Django has a builtin development server; try it on your local machine first.
The Django tutorial seems to assume that you are running some Unix flavor (like Linux) on your local machine (a Mac would be fine too). If you are running some MS Windows, this page may help. Don't be scared of the command line, setup is only once.
The Django admin app is worth the trouble!
Indeed PHP is synonimous of cheap hosting and very well supported. Write some crap and run. Python support is not as pervasive; at some hosting providers, working with Python is almost as easy as with PHP, and most have instructions about Django in their FAQs.
The mechanics is different: Python frameworks don't reload with each page load like PHP, there is an application always running that responds to HTTP requests. That is why you are advised to start on your local machine, just to acquaint the new paradigm.
I currently work with .NET exclusively and would like to have a go at python. To this end I need to set up a python development environment. I guide to this would be handy. I guess I would be doing web development so will need a web server and probably a database. I also need pointers to popular ORM's, an MVC framework, and a testing library.
One of my main criteria with all this is that I want to understand how it works, and I want it to be as isolated as possible. This is important as i am wary of polluting what is a working .NET environment with 3rd party web and database servers. I am perfectly happy using SQLite to start with if this is possible.
If I get on well with this I am also likely to want to set up automated build and ci server (On a virtual machine, probably ubuntu). Any suggestions for these would be useful.
My ultimate aim if i like python is to have similar sorts of tools that i have available with .NET and to really understand the build and deployment of it all. To start with I will settle for a simple development environment that is as isolated as possible and will be easy to remove if I don't like it. I don't want to use IronPython as I want the full experience of developing a python solution using the tools and frameworks that are generally used.
It's not that hard to set up a Python environment, and I've never had it muck up my .NET work. Basically, install Python --- I'd use 2.6 rather than 3.0, which is not yet broadly accepted --- and add it to your PATH, and you're ready to go with the language. I wouldn't recommend using a Ubuntu VM as your development environment; if you're working on Windows, you might as well develop on Windows, and I've had no significant problems doing so. I go back and forth from Windows to Linux with no trouble.
If you have an editor that you're comfortable with that has basic support for Python, I'd stick with it. If not, I've found Geany to be a nice, light, easy-to-use editor with good Python support, though I use Emacs myself because I know it; other people like SCITE, NotePad++, or any of a slew of others. I'd avoid fancy IDEs for Python, because they don't match the character of the language, and I wouldn't bother with IDLE (included with Python), because it's a royal pain to use.
Suggestions for libraries and frameworks:
Django is the standard web framework, but it's big and you have to work django's way; I prefer CherryPy, which is also actively supported, but is light, gives you great freedom, and contains a nice, solid webserver that can be replaced easily with httpd.
Django includes its own ORM, which is nice enough; there's a standalone one for Python, though, which is even nicer: SQL Alchemy
As far as a testing library goes, pyunit seems to me to be the obvious choice
Good luck, and welcome to a really fun language!
EDIT summary: I originally recommended Karrigell, but can't any more: since the 3.0 release, it's been continuously broken, and the community is not large enough to solve the problems. CherryPy is a good substitute if you like a light, simple framework that doesn't get in your way, so I've changed the above to suggest it instead.
Well, if you're thinking of setting up an Ubuntu VM anyway, you might as well make that your development environment. Then you can install Apache and MySQL or Postgres on that VM just via the standard packaging tools (apt-get install), and there's no danger of polluting your Windows environment.
You can either do the actual development on your Windows machine via your favourite IDE, using the VM as a networked drive and saving the code there, or you can just use the VM as a full desktop environment and do everything there, which is what I would recommend.
Install the pre-configured ActivePython release from activestate.
Among other features, it includes the PythonWin IDE (Windows only) which makes it easy to explore Python interactively.
The recommended reference is Dive Into Python, mentioned many times on similar SO discussions.
You should install python 2.4, python 2.5, python 2.6 and python 3.0, and add to your path the one you use more often (Add c:\Pythonxx\ and c:\Pythonxx\Scripts).
For every python 2.x, install easy_install; Download ez_setup.py and then from the cmd:
c:\Python2x\python.exe x:\path\to\ez_setup.py
c:\Python2x\Scripts\easy_install virtualenv
Then each time you start a new project create a new virtual environment to isolate the specific package you needs for your project:
mkdir <project name>
cd <project name>
c:\Python2x\Scripts\virtualenv --no-site-packages .\v
It creates a copy of python and its libraries in .v\Scripts and .\v\Lib. Every third party packages you install in that environment will be put into .\v\Lib\site-packages. The -no-site-packages don't give access to the global site package, so you can be sure all your dependencies are in .\v\Lib\site-packages.
To activate the virtual environment:
.\v\Scripts\activate
For the frameworks, there are many. Django is great and very well documented but you should probably look at Pylons first for its documentions on unicode, packaging, deployment and testing, and for its better WSGI support.
For the IDE, Python comes with IDLE which is enough for learning, however you might want to look at Eclipse+PyDev, Komodo or Wingware Python IDE. Netbean 6.5 has beta support for python that looks promising (See top 5 python IDE).
For the webserver, you don't need any; Python has its own and all web framework come with their own. You might want to install MySql or ProgreSql; it's often better to develop on the same DB you will use for production.
Also, when you have learnt Python, look at Foundations of Agile Python Development or Expert Python Programming.
Using Python on Windows
SO: Python tutorial for total beginners?
Take a look at Pylons, read about WSGI and Paste.
There's nice introductory Google tech talk about them: ReUsable Web Components with Python and Future Python Web Development.
Here's my answer to similar question:
Django vs other Python web frameworks?
NOTE: I included a lot of links to frameworks, projects and what-not, but as a new user I was limited to 1 link per answer. If someone else with enough reputation to edit wants/can edit them into this answer instead of the footnotes, I'd be grateful.
There are some Python IDE's such as Wing IDE[1], I believe some people use Eclipse[2] with a python plugin[3] as well. A lot of people in the #python channel of FreeNode seem to prefer vim, emacs, nano and similar text editors in favor of IDE's. My personal preffered editor is Vim, but if you've mostly done .NET development on windows, presumably with the usual Visual X IDE's, vim and emacs will probably cause you culture shock and you'd be better of using an IDE.
Nearly all python web frameworks* support the WSGI standard[4], most of the large web servers have some sort of plugin to support WSGI, the others support WSGI via fast cgi or plain cgi.
The Zope[5] and Django[6] frameworks have their own ORM's, of other ORM's the two most well known appear to be SQL Alchemy[7] and SQL Object[8]. I only have experience with the former, but both support all possible sane database choices, including SQLite which is installed together with Python and hence perfectly suited to testing and experimenting without polluting your .NET environment with 3rd part web servers and database servers.
The builtin unittest[9] and pyunit[10] frameworks seem to be the preffered solutions for unit testing, but I don't have much experience with these.
bpython[11] and ipython[12] offer enhanced interactive python shells which can greatly help speed up and testing small bits of code and hence worth looking in to.
As for a list of well known and often used web frameworks, look into the following frameworks**:
Twisted[13] is a generic networking framework, which supports almost every single protocol under the sun.
Pylons[14] is light-weight framework aimed at being as flexible as possible and leaving all the choices about what ORM, templating language and what-not to you.
CherryPy[15] tries to provide an interface to expose Python objects to the web.
Django[6] attempts to be an all-in-one solution, builtin template system, ORM, admin pages and internationalization. While the previous frameworks have more DIY wiring together various frameworks work involved with them.
Zope[5] is aimed to be suitable for large enterprise applications, I've heard nothing but good things about it, but consensus seems to be that for smaller you're probably better off with one of the simpler and smaller frameworks.
TurboGears[16] is the framework I know the least about, but it seems to be mostly competition for Django.
This is everything I can think of right now, I'll edit and add stuff if I can think of it. I hope this helps you some in the wonderful world of python.
* - The main exception would be Apache's mod_python, which you should avoid for exactly that reason, use mod_wsgi instead.
** - Word of warning, I have not personally used these frameworks this is just a very short impression I have gotten from talking to other people about each framework, it may be wildly inaccurate. (If anyone has any corrections, do comment and I'll try to edit and fix this answer).
(The http:// is missing since they're recognized as links otherwise)
[1] www.wingware.com/
[2] www.eclipse.org/
[3] pydev.sourceforge.net/
[4] wsgi.org/wsgi/
[5] www.zope.org/
[6] www.djangoproject.com/
[7] www.sqlalchemy.org/
[8] www.sqlobject.org/
[9] docs.python.org/library/unittest.html
[10] pyunit.sourceforge.net/pyunit.html
[11] www.bpython-interpreter.org/
[12] ipython.scipy.org/
[13] twistedmatrix.com/trac/
[14] pylonshq.com/
[15] www.cherrypy.org/
[16] turbogears.org/
Environment?
Here is the simplest solution:
Install Active Python 2.6. Its the Python itself, but comes with some extra handy useful stuff, like DiveintoPython chm.
Use Komodo Edit 5. It is among the good free editor you can use for Python.
Use IDLE. Its the best simplest short snippet editor, with syntax highlighting and auto complete unmatched by most other IDEs. It comes bundled with python.
Use Ipython. Its a shell that does syntax highlighting and auto complete, bash functions, pretty print, logging, history and many such things.
Install easy_install and/or pip for installing various 3rd party apps easily.
Coming from Visual Studio and .Net it will sound a lot different, but its an entirely different world.
For the framework, django works the best. Walk thro the tutorial and you will be impressed enough. The documentation rocks. The community, you have to see for yourself, to know how wonderful it is!!
Python has build in SQL like database and web server, so you wouldn't need to install any third party apps. Remember Python comes with batteries included.
If you've worked with Eclipse before you could give Pydev a try