In this recipe, you'll build your first Flask application in 10 minutes or less. Flask is a Python microframework that you can use to create simple web-based applications very quickly. Visit http://flask.pocoo.org/ for all the details.
In this recipe, we're going to keep it simple; however, included with the book is a fully functional Flask application chock-full of the latest JavaScript libraries and CSS themes, ready for customization.
In order to write Flask applications, you first need to install it. If you have the Anaconda distribution of Python, you'll already have it. Just in case you don't, open a terminal and run the following:
pip install flask
To be able to pull data directly from MongoDB later on, install the flask-mongoengine
library with this command:
pip install flask-mongoengine
Now let's build that first application.
templates
.index.py
file.templates
folder, and create a file named index.html
.first-flask-app - index.py - templates - index.html
index.py,
and add the following code:from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): message = 'Welcome to Your First Flask Application!' return render_template('index.html', message=message) if __name__ == "__main__": app.debug = True # Comment this out when going into production app.run()
index.html
file in the templates folder and add this code:<h1>{{ message }}</h1>
python index.py
This will start Flask's internal web server and run it on port 5000
. Open up a web browser and navigate to http://127.0.0.1:5000
.
The first thing that we do is to import Flask and the render_template
methods from the Flask
library. This allows us to create a Flask application as well as render templates. Templates are what will have data inserted into them and be shown to the users when they visit the application:
from flask import Flask, render_template
Next we create a new Flask application object:
app = Flask(__name__)
In order for Flask to serve a web page, it needs to know what to do when a user visits a URL. These two lines define a route to the home page as well as an index
function that would run when that URL is visited. In this case, we define a message
variable, assign it a value, then tell Flask to render the index.html
template, and assign the value of message
to the message variable in the template:
@app.route('/') def index(): message = 'Welcome to Your First Flask Application!' return render_template('index.html', message=message)
In order to run the file at the command line as we did, we need to tell it what to run. In this instance, we are telling Python to run the Flask application that we previously defined, and turn on debugging before doing so:
if __name__ == "__main__": app.debug = True # Comment this out when going into production app.run()
When you run the file using the Python index.py
command, you will see something similar to the following screenshot :
Congratulations! You have created your first Flask application.
For much more on using Flask, I highly suggest the following video and book from Packt Publishing: