flask
Python hosting: Host, run, and code Python in the cloud!
In this tutorial you’ll learn how to build a web app with Python. We’ll use a micro-framework called Flask.
Why Flask?
- easy to use.
- built in development server and debugger
- integrated unit testing support
- RESTful request dispatching
- uses Jinja2 templating
- support for secure cookies (client side sessions)
- 100% WSGI 1.0 compliant
- Unicode based
- extensively documented
Related course:
Python Flask: Make Web Apps with Python
$ pip install Flask |
Create a file called hello.py
from flask import Flask |
Finally run the web app using this command:
$ python hello.py |
Open http://localhost:5000/ in your webbrowser, and “Hello World!” should appear.
Creating URL routes
URL Routing makes URLs in your Web app easy to remember. We will now create some URL routes:
/hello |
Copy the code below and save it as app.py
from flask import Flask |
Restart the application using:
$ python hello.py |
Try the URLs in your browser:
- http://127.0.0.1:5000/
- http://127.0.0.1:5000/hello
- http://127.0.0.1:5000/members
- http://127.0.0.1:5000/members/Jordan/
Related course:
Python Flask: Make Web Apps with Python
Style Flask Pages
We will separate code and User Interface using a technique called Templates. We make the directory called /templates/ and create the template:
|
The Python Flask app with have a new URL route. We have changed the default port to 80, the default HTTP port:
from flask import Flask, flash, redirect, render_template, request, session, abort |
You can then open : http://127.0.0.1/hello/Jackson/
Styling the template
Do you want some better looking template? We modify the file:
{% extends "layout.html" %} |
We then create layout.html which defines the look of the page. (You may want to split the stylesheet and layout.html file). Copy this as layout.html
|
Restart the App and open the url. http://127.0.0.1/hello/Jackson/
You can pick any name other than Jackson.
Related course:
Python Flask: Make Web Apps with Python
Passing Variables
Lets display random quotes instead of always the same quote. We will need to pass both the name variable and the quote variable. To pass multiple variables to the function, we simply do this:
return render_template( |
Our new test.html template will look like this:
{% extends "layout.html" %} |
We will need to pick a random quote. To do so, we use this code:
quotes = [ "'If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.' -- John Louis von Neumann ", |
The first thing you see is we have defined an array of multiples quotes. These can be accessed as quote[0], quote[1], quote[2] and so on. The function randint() returns a random number between 0 and the total number of quotes, one is subtracted because we start counting from zero. Finally we set the quote variable to the quote the computer has chosen. Copy the code below to app.py:
from flask import Flask, flash, redirect, render_template, request, session, abort |
When you restart the application it will return one of these quotes at random.
Download App and more Flask Examples
Whats next?
You could link your site with a database system such as MySQL, MariaDb or SQLite. You can find an SQLite tutorial here. Enjoy creating your application!
Leave a Reply:
I'll like to thank you a lot for a giving me a start, because before reading this, I have no idea on where to start from, I;m really grateful...
You're welcome. More tutorials coming soon
I get an "Internal Server Error" when I open http://127.0.0.1/hello/Jackson/
Found my mistake.
I put the test.html file in the same directory as hello.py. It needs to be in the templates subdirectory
I get an "ImportError: No module named flask" when I execute the command.
Hi Tom, this means you do not have the flask package installed. Are you on a desktop version? Flask is not available on a web interpreter.
To install flask you can run:
pip install flask
If you are on windows, you should install pip first. A tutorial here:
https://www.youtube.com/watch?v=zPMr0lEMqpo
Alternative: Pycharm seems to support Flask by default. https://www.jetbrains.com/pycharm/help/creating-flask-project.html
I get an "ImportError: No module named request" when I execute the command.
Hi, request is part of Flask. Are you using a desktop/console version of Python? Flask will not work on a web-python version.
It looks like an installation problem. Which os do you use? Do you use Python from console or an ide like PyCharm? Try reinstalling Flask using:
pip install flask
or
pip3 install flask
This code is tested with Python 3.4.0 and Flask 0.10.1 and Python 2.7.
Hi Frank,if I install python3.x from python.org
If I want to uninstall it , what should i do?
I try to put it into trash but it seems not work
By the way I'm using mac os
it seems that the problem is I'm using python2.7
when I add up #!usr/bin/env python3.4 it seems work until 'Creating Awesome pages'
I do not recommend uninstalling Python because you can have several versions of python on your machine without conflicts.
There is no default uninstaller for mac os. You can uninstall python3 from the command line. The steps to remove python 3 are here (simply replace 2.7 with 3):
http://stackoverflow.com/questions/3819449/how-to-uninstall-python-2-7-on-a-mac-os-x-10-6-4
Hi, that's good news. Which error do you get at 'Creating Awesome pages'?
Perhaps the template directory is missing, that one should have a directory /templates/ with the file test.html such that
Thank you very much!
Thanks for the tutorial. I think I found a typo - below we should be running $ python hello.py
Interesting tutorial. And I am unable to get the last code to run - either it's my slow brain (either too much or not enough coffee) or a problem with file names. I am working on an Ubuntu VM.
Which error do you get?
It's important to have the templates in the /templates/ directory. The template directory is inside the python code directory.
In this case it's named test.html for both the template and in the url (render_template)
could you display the directory format for this tutorial. all explain this route little more
/hello
/members/
/members/name/
where it should be created
when I created app.ph and inserted the code app
You should create the subdirectories /hello /members in the root directory of your project. Make sure the file is .py and not .ph and use spaces for indention
I am new to Python, so I was looking for a beginners tutorial.
Still I was able to spot the issue with the initial hello.py example in line 9. :-)
Thank you for the good explanations, I like the steep learning curve it is providing.
Good stuff.