Python on the Web

From BC Wiki
Jump to: navigation, search

Installing Apache and Flask[edit]

sudo apt-get install libapache2-mod-wsgi

Configuring Your Application[edit]

Next I created flaskTest.wsgi in my application's folder:

	import sys
	from flaskTest import app as application

I created a new Apache VirtualHost config in /etc/apache2/sites-available:

	<VirtualHost *:80>
		WSGIDaemonProcess flaskTest user=flask group=www-data threads=5 home=/<redacted>/flaskTest
		WSGIScriptAlias / /<redacted>/flaskTest/flaskTest.wsgi
		<Directory /<redacted>/flaskTest>
		    WSGIProcessGroup flaskTest
		    WSGIApplicationGroup %{GLOBAL}
		    WSGIScriptReloading On
		    Order deny,allow
		    Allow from all

Adding the WSGIScriptReloading directive meant that any time I made changes to my application files, I could simply $ touch flaskTest.wsgi and the app would restart with changes applied. I even found a python-based implementation of touch that I could use if I wanted to be able to restart the app from within the app!

As mentioned above, by default, the application starts as if it were called from the server root. The WSGI Documentation gave me the extra home option for the WSGIDaemonProcess directive, which changes that to wherever you want it.

Send Data from a Textbox[edit]

To Sort[edit]


sudo apt-get install python-flask

Python Quickstart[edit]

from flask import Flask
app = Flask(__name__)

def hello():
    return "Hello World!"

if __name__ == "__main__":


How to download a file generated on-the-fly in Flask

# We need to modify the response, so the first thing we # need to do is create a response out of the CSV string response = make_response(csv)

# This is the key: Set the right header for the response # to be downloaded, instead of just printed on the browser response.headers["Content-Disposition"] = "attachment; filename=books.csv" return response

Apache, WSGI, and Flask[edit]

Here's a very straightforward tutorial:

and a more complicated official tutorial:

Python Graphics Libraries[edit]

Python Library for Simple Drawing (pygame)

Python Tutorial: Graphics

PyQGraph - Scientific Graphics and GUI Library for Python

Python - Useful Modules

PyX - Python Graphics Package

Python Web Frameworks[edit]

Python - Flask Web Development Tutorial

4 Python Web Frameworks Compared