python logo


Category: flask

flask hello world

In this tutorial you’ll learn how to build a web app with Python.

We’ll use a micro-framework called Flask. It has a small core but is extensible with many plugins such as SQLAlchemy, Babel, CouchDB, MongoDB etc.

Some Flask example apps are:

Related course
Python Flask: Make Web Apps with Python

  • 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

Download Flask Examples

Installing Flask

Install Flask using the command below:

pip install Flask

Flask hello world app


Create a file called hello.py


from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

if __name__ == "__main__":
app.run()

Finally run the web app using this command:

$ python hello.py
* Running on http://localhost:5000/

Open http://localhost:5000/ in your webbrowser, and “Hello World!” should appear.

Download Flask Examples

Flask with static html files

You can use the Flask framework and use static files together.

Flask will give you URL routing, many features and all the Python benefits.

You may want an application that is partly dynamic and partly static. Or you may simply want to browse with URL routing. In this article we will teach you how to load static HTML files with Flask.

Related course
Python Flask: Make Web Apps with Python

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/<string:page_name>/')
def render_static(page_name):
return render_template('%s.html' % page_name)

if __name__ == '__main__':
app.run()

This application initializes a Flask app with the method:


app.run()

The app creates an URL route for any possible page and links that to static html files with:


@app.route('/<string:page_name>/')
def render_static(page_name):
return render_template('%s.html' % page_name)

</string:page_name>

Create a directory /templates/ and add the file hello.html:



<title>Hello World Template</title>

Hello World


Start the server with:

$ python app.py
* Running on http://127.0.0.1:5000/

Then any .html file is accesible using URL routing.

For example the static file hello.html can be accessed using http://127.0.0.1:5000/hello. You can store any css file in the /static/ directory.

Download Flask Examples

jinja2 tutorial

chart flask

flask

Login authentication with Flask

flask form

Login to Flask App with Twitter

Login to your Flask app with Google

Flask, JSON and the Google Charts API

flask boilerplate