An Introduction to Python’s Flask Framework

An Introduction to Python’s Flask Framework

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 30 Minutes

Flask is a small and powerful web framework for Python. It’s easy to learn and simple to use, enabling you to build your web app in a short amount of time.

In this article, I’ll show you how to build a simple website, containing two static pages with a small amount of dynamic content. While Flask can be used for building complex, database-driven websites, starting with mostly static pages will be useful to introduce a workflow, which we can then generalize to make more complex pages in the future. Upon completion, you’ll be able to use this sequence of steps to jumpstart your next Flask app.


Installing Flask

Before getting started, we need to install Flask. Because systems vary, things can sporadically go wrong during these steps. If they do, like we all do, just Google the error message or leave a comment describing the problem.

Install virtualenv

Virtualenv is a useful tool that creates isolated Python development environments where you can do all your development work.

We’ll use virtualenv to install Flask. Virtualenv is a useful tool that creates isolated Python development environments where you can do all your development work. Suppose you come across a new Python library that you’d like to try. If you install it system-wide, there is the risk of messing up other libraries that you might have installed. Instead, use virtualenv to create a sandbox, where you can install and use the library without affecting the rest of your system. You can keep using this sandbox for ongoing development work, or you can simply delete it once you’ve finished using it. Either way, your system remains organized and clutter-free.

It’s possible that your system already has virtualenv. Refer to the command line, and try running:

$ virtualenv --version

If you see a version number, you’re good to go and you can skip to this “Install Flask” section. If the command was not found, use easy_install or pip to install virtualenv. If running Linux or Mac OS X, one of the following should work for you:

$ sudo easy_install virtualenv

or:

$ sudo pip install virtualenv

or:

$ sudo apt-get install python-virtualenv

If you don’t have either of these commands installed, there are several tutorials online, which will show you how to install it on your system. If you’re running Windows, follow the “Installation Instructions” on this page to get easy_install up and running on your computer.

Install Flask

After installing virtualenv, you can create a new isolated development environment, like so:

$ virtualenv flaskapp

Here, virtualenv creates a folder, flaskapp/, and sets up a clean copy of Python inside for you to use. It also installs the handy package manager, pip.

Enter your newly created development environment and activate it so you can begin working within it.

$ cd flaskapp
$ . bin/activate

Now, you can safely install Flask:

$ pip install Flask

Setting up the Project Structure

Let’s create a couple of folders and files within flaskapp/ to keep our web app organized.

.
.
├── app
│   ├── static
│   │   ├── css
│   │   ├── img
│   │   └── js
│   ├── templates
│   ├── routes.py
│   └── README.md

Within flaskapp/, create a folder, app/, to contain all your files. Inside app/, create a folder static/; this is where we’ll put our web app’s images, CSS, and JavaScript files, so create folders for each of those, as demonstrated above. Additionally, create another folder, templates/, to store the app’s web templates. Create an empty Python file routes.py for the application logic, such as URL routing.

And no project is complete without a helpful description, so create a README.md file as well.

Now, we know where to put our project’s assets, but how does everything connect together? Let’s take a look at “Fig. 1″ below to see the big picture:

Fig. 1

  1. A user issues a request for a domain’s root URL / to go to its home page
  2. routes.py maps the URL / to a Python function
  3. The Python function finds a web template living in the templates/ folder.
  4. A web template will look in the static/ folder for any images, CSS, or JavaScript files it needs as it renders to HTML
  5. Rendered HTML is sent back to routes.py
  6. routes.py sends the HTML back to the browser

We start with a request issued from a web browser. A user types a URL into the address bar. The request hits routes.py, which has code that maps the URL to a function. The function finds a template in the templates/ folder, renders it to HTML, and sends it back to the browser. The function can optionally fetch records from a database and then pass that information on to a web template, but since we’re dealing with mostly static pages in this article, we’ll skip interacting with a database for now.

Now that we know our way around the project structure we set up, let’s get started with making a home page for our web app.


Creating a Home Page

When you write a web app with a couple of pages, it quickly becomes annoying to write the same HTML boilerplate over and over again for each page. Furthermore, what if you need to add a new element to your app, such as a new CSS file? You would have to go into every single page and add it in. This is time consuming and error prone. Wouldn’t it be nice if, instead of repeatedly writing the same HTML boilerplate, you could define your page layout just once, and then use that layout to make new pages with their own content? This is exactly what web templates do!

Web templates are simply text files that contain variables and control flow statements (if..else, for, etc), and end with an .html or .xml extension.

The variables are replaced with your content, when the web template is evaluated. Web templates remove repetition, separate content from design, and make your application easier to maintain. In other, simpler words, web templates are awesome and you should use them! Flask uses the Jinja2 template engine; let’s see how to use it.

As a first step, we’ll define our page layout in a skeleton HTML document layout.html and put it inside the templates/ folder:

app/templates/layout.html

<!DOCTYPE html>
<html>
  <head>
    <title>Flask App</title>    
  </head>
  <body>
  
    <header>
      <div class="container">
        <h1 class="logo">Flask App</h1>
      </div>
    </header> 
    
    <div class="container">
      {% block content %}
      {% endblock %}
    </div>
    
  </body>
</html>

This is simply a regular HTML file…but what’s going on with the {% block content %}{% endblock %} part? To answer this, let’s create another file home.html:

app/templates/home.html

{% extends "layout.html" %}
{% block content %}
  <div class="jumbo">
    <h2>Welcome to the Flask app<h2>
    <h3>This is the home page for the Flask app<h3>
  </div>
{% endblock %}

The file layout.html defines an empty block, named content, that a child template can fill in. The file home.html is a child template that inherits the markup from layout.html and fills in the “content” block with its own text. In other words, layout.html defines all of the common elements of your site, while each child template customizes it with its own content.

This all sounds cool, but how do we actually see this page? How can we type a URL in the browser and “visit” home.html? Let’s refer back to Fig. 1. We just created the template home.html and placed it in the templates/ folder. Now, we need to map a URL to it so we can view it in the browser. Let’s open up routes.py and do this:

app/routes.py

from flask import Flask, render_template

app = Flask(__name__)      

@app.route('/')
def home():
  return render_template('home.html')

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

That’s it for routes.py. What did we do?

  1. First. we imported the Flask class and a function render_template.
  2. Next, we created a new instance of the Flask class.
  3. We then mapped the URL / to the function home(). Now, when someone visits this URL, the function home() will execute.
  4. The function home() uses the Flask function render_template() to render the home.html template we just created from the templates/ folder to the browser.
  5. Finally, we use run() to run our app on a local server. We’ll set the debug flag to true, so we can view any applicable error messages if something goes wrong, and so that the local server automatically reloads after we’ve made changes to the code.

We’re finally ready to see the fruits of our labor. Return to the command line, and type:

$ python routes.py

Visit http://localhost:5000/ in your favorite web browser.

When we visited http://localhost:5000/, routes.py had code in it, which mapped the URL / to the Python function home(). home() found the web template home.html in the templates/ folder, rendered it to HTML, and sent it back to the browser, giving us the screen above.

Pretty neat, but this home page is a bit boring, isn’t it? Let’s make it look better by adding some CSS. Create a file, main.css, within static/css/, and add these rules:

static/css/main.css

body {
  margin: 0;
  padding: 0;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #444;
}

/*
 * Create dark grey header with a white logo
 */
 
header {
  background-color: #2B2B2B;
  height: 35px;
  width: 100%;
  opacity: .9;
  margin-bottom: 10px;
}

header h1.logo {
  margin: 0;
  font-size: 1.7em;
  color: #fff;
  text-transform: uppercase;
  float: left;
}

header h1.logo:hover {
  color: #fff;
  text-decoration: none;
}

/*
 * Center the body content
 */
 
.container {
  width: 940px;
  margin: 0 auto;
}

div.jumbo {
  padding: 10px 0 30px 0;
  background-color: #eeeeee;
  -webkit-border-radius: 6px;
     -moz-border-radius: 6px;
          border-radius: 6px;
}

h2 {
  font-size: 3em;
  margin-top: 40px;
  text-align: center;
  letter-spacing: -2px;
}

h3 {
  font-size: 1.7em;
  font-weight: 100;
  margin-top: 30px;
  text-align: center;
  letter-spacing: -1px;
  color: #999;
}

Add this stylesheet to the skeleton file layout.html so that the styling applies to all of its child templates by adding this line to its <head> element:

      <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">;

We’re using the Flask function, url_for, to generate a URL path for main.css from the static folder. After adding this line in, layout.html should now look like:

app/templates/layout.html

<!DOCTYPE html>
<html>
  <head>
    <title>Flask</title>    
    <strong><link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}"></strong>
  </head>
  <body>
    <header>
      <div class="container">
        <h1 class="logo">Flask App</h1>
      </div>
	  </header>
	
    <div class="container">
      {% block content %}
      {% endblock %}
    </div>
  </body>
</html>

Let’s switch back to the browser and refresh the page to view the result of the CSS.

That’s more like it! Now, when we visit http://localhost:5000/, routes.py still maps the URL / to the Python function home(), and home() still finds the web template home.html in the templates/ folder. But, since we added the CSS file main.css, the web template home.html looks in static/ to find this asset, before rendering to HTML and being sent back to the browser.

We’ve achieved a lot so far. We started with Fig. 1 by understanding how Flask works, and now we’ve seen how it all plays out, by creating a home page for our web app. Let’s move on and create an About page.


Creating an About Page

In the previous section, we created a web template home.html by extending the skeleton file layout.html. We then mapped the URL / to home.html in routes.py so we could visit it in the browser. We finished things up by adding some styling to make it look pretty. Let’s repeat that process again to create an about page for our web app.

We’ll begin by creating a web template, about.html, and putting it inside the templates/ folder.

app/templates/about.html

{% extends "layout.html" %}
 
{% block content %}
  <h2>About</h2>
  <p>This is an About page for the Intro to Flask article. Don't I look good? Oh stop, you're making me blush.</p>
{% endblock %}

Just like before with home.html, we extend from layout.html, and then fill the content block with our custom content.

In order to visit this page in the browser, we need to map a URL to it. Open up routes.py and add another mapping:

from flask import Flask, render_template
 
app = Flask(__name__)
 
@app.route('/')
def home():
  return render_template('home.html')
 
@app.route('/about')
def about():
  return render_template('about.html')
 
if __name__ == '__main__':
  app.run(debug=True)

We mapped the URL /about to the function about(). Now we can open up the browser and go to http://localhost:5000/about and check out our newly created page.


Adding Navigation

Most websites have links to their main pages within the header or footer of the document. These links are usually visible across all pages of a website. Let’s open up the skeleton file, layout.html. and add these links so they show up in all of the child templates. Specifically, let’s add a <nav> element inside the <header> element:

app/templates/layout.html

...
<header>
  <div class="container">
    <h1 class="logo">Flask App</h1>
    <strong><nav>
      <ul class="menu">
        <li><a href="{{ url_for('home') }}">Home</a></li>
        <li><a href="{{ url_for('about') }}">About</a></li>
      </ul>
    </nav></strong>
  </div>
</header>
...

Once again, we use the Flask function url_for to generate URLs.

Next, add some more style rules to main.css to make these new navigation elements look good:

app/static/css/main.css

...

/*
 * Display navigation links inline
 */

.menu {
  float: right;
  margin-top: 8px;
}

.menu li {
  display: inline;
}

.menu li + li {
  margin-left: 35px;
}

.menu li a {
  color: #999;
  text-decoration: none;
}

Finally, open up the browser and refresh http://localhost:5000/ to see our newly added navigation links.


Conclusion

Over the course of this article, we built a simple web app with two, mostly static, pages. In doing so, we learned a workflow that can be used to create more complex websites with dynamic content. Flask is a simple, but powerful framework that enables you to efficiently build web apps. Go ahead – check it out!

Tags: flask
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • ATMD

    Nice to see such a great framework getting some net tuts love

  • matesnake

    The best python framework ever = ) !!!

  • http://www.facebook.com/joseph.freemind Joseph Freemind

    Nice to see another python tutorial again after a long period of time. net tuts focus more on PHP and Ruby than Python. Thank you.

    • http://www.facebook.com/doctor.chicha Adel Totott

      We <3 python : )

      • pygame1

        Yeah!

  • http://www.butenas.com/ Ignas B.

    It was just matter of time when we will see Flask here in Nettuts. Flask is an awesome framework and it is very fun to work with it. Highly recommend to try :)

    Btw there are some formatting problems in article and also “Tags” at the bottom of the article says it is about “Flash” not “Flask” :)

  • http://twitter.com/EduWebGuy EduWebGuy

    I’m just getting into Python via the Django framework. How is Flask different/better than Django?

    • MPinteractiv

      Django comes with battery included , meaning it (some) makes choices for you but you spend time only on writing the business logic and learning the rest. Flask has none , meaning you need to build your own stack.

      • http://twitter.com/EduWebGuy EduWebGuy

        Thanks. That’s what I figured.

      • http://www.facebook.com/yashodhan.kulkarni Yashodhan Sunilrao Kulkarni

        Have anyone tried hands on Webnotes python framework? https://github.com/webnotes/wnframework

        It offers browser based application development.. sounds cool.. :)

      • edisonx

        How is Flask different/better than Pyramid?

      • http://www.facebook.com/jesus.bejarano.948 Jesus Bejarano

        Django : full stack
        Pyramid: is full stack but less clutered than django
        Flask: Is more a framework/librery but you should have more flexibility since you could build the your stack to fit exactly your needs and projects.

        No of them is better than the other, they just follow distincts approches.

      • http://www.blaiselaflamme.com blaflamme

        Pyramid is not near «full stack» as Flask is. Pyramid could be as minimal as Flask (with more packages dependencies) and have even less opinions than Flask. So Pyramid lets you build your stack to fits your needs and also lets your organize your project the way you want.

  • albertogrespan

    Great tutorial, I’ve been working on a flask application skeleton minimize the time of starting a project with flask. Here is the link https://github.com/albertogg/flask-bootstrap

  • sirfilip

    <3 Flask. I see it as sinatra for python had a chance to get my feet wet and play with it. Great tut and great micro framework.

  • http://www.facebook.com/jasetay Jase Yao Tay

    im a beginner.. i got this error code… right before and after adding my css.. any help would be appreciated

    Traceback (most recent call last):
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1701, in __call__
    return self.wsgi_app(environ, start_response)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1687, in wsgi_app
    response = self.full_dispatch_request()
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1358, in full_dispatch_request
    rv = self.dispatch_request()
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/app.py”, line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File “/home/jase/flaskapp/app/routes.py”, line 5, in home
    return render_template (‘home.html’)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/flask/templating.py”, line 124, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
    File “/home/jase/flaskapp/lib/python2.7/site-packages/jinja2/environment.py”, line 758, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/jinja2/environment.py”, line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
    File “/home/jase/flaskapp/lib/python2.7/site-packages/jinja2/environment.py”, line 693, in _load_template
    template = self.loader.load(self, name, globals)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/jinja2/loaders.py”, line 127, in load
    code = environment.compile(source, name, filename)
    File “/home/jase/flaskapp/lib/python2.7/site-packages/jinja2/environment.py”, line 493, in compile
    self.handle_exception(exc_info, source_hint=source)
    File “/home/jase/flaskapp/app/templates/home.html”, line 1, in template
    {% extends “layout.html” % }
    TemplateSyntaxError: unexpected ‘}’

    • http://twitter.com/cuttarug cuttarug

      Looks like there is a space between the closing ‘%’ and ‘}’. Remove it so that line 1 of home.html looks like this: ‘{% extends “layout.html” %}’

      • http://www.facebook.com/jasetay Jase Yao Tay

        yeah thanks.. i figured it out and completed the exercise. should’ve removed this immediately..

  • http://twitter.com/chichibek chichibek bros

    i like your OS

  • http://twitter.com/slafs Sławek Ehlert

    Nice. As a side note to virtualenv part I really recommend using virtualenvwrapper.

  • pixelBender67

    Cool

  • chris simmons

    I’ve recently been getting into Python and chose Flask as my web framework of choice. Been very much enjoying it so far! I’d love to see more tutorials focusing on the great extensions available for it. Perhaps something covering actually deploying your app on a service such as Heroku.

    • Lalith Polepeddi
      Author

      Yea I agree, Flask is an enjoyable framework. I have a follow up article about using Flask extensions that will come out soon.

    • Lalith Polepeddi
      Author

      I have an article about how to deploy Flask to Heroku here.

  • Zack

    {% extends “layout.html” %}
    {% block content %}
    About us
    This is a sample app for the Flask tutorial. Don’t I look good? Oh stop, you’re making me blush.
    {% endblock %}

    *incorrect h1 tag should be

    {% extends “layout.html” %}
    {% block content %}
    About us
    This is a sample app for the Flask tutorial. Don’t I look good? Oh stop, you’re making me blush.
    {% endblock %}

    CSS File

    h2 {
    font-size: 3em;
    margin-top: 40px;
    text-align: center;
    letter-spacing: -2px;

    }

    h3 {
    font-size: 1.7em;
    font-weight: 100;
    margin-top: 30px;
    text-align: center;
    letter-spacing: -1px;
    color: #999;
    }

    * Should be

    h1 {

    font-size: 3em;
    margin-top: 40px;
    text-align: center;
    letter-spacing: -2px;
    }

    h2 {
    font-size: 1.7em;
    font-weight: 100;
    margin-top: 30px;
    text-align: center;
    letter-spacing: -1px;
    color: #999;
    }

  • Guest

    Templating engine is looking very similar to Twig bundled with Symfony 2.0. Same function names etc (block, extends, url_for).

    • Mark Ross

      The template engine is Jinja – Twig is based on Jinja.

      • http://twitter.com/jontybehr Jonty Behr

        Actually, twig came before Jinja. See my comment above.

    • http://twitter.com/jontybehr Jonty Behr

      Thats no coincidence. Twig was originally written by Armin Ronacher, and then inherited by fabpot. Guess who the author of Jinja (and Flask) is? Armin Ronacher.

  • http://www.facebook.com/simonwilder Simon Wilder

    I agree too. Very nice to see Python back on the site. I’m getting back into Django and love it. May look into Flask for smaller sites. ty

  • http://twitter.com/smith_quintin Quintin Smith

    It would be great at the conclusion to provide one or two ways of deployment. That would be really sweet@!

    • Lalith Polepeddi
      Author

      Thanks for the idea. I have an article on how to deploy Flask to Heroku here.

  • Liban Shire

    I’m on mac OS X 10.8 and when I run the command $ python routes.py[/python] in the terminal it gives met this error:

    command not found,

    when I try again I get this error:

    from flask import Flask, render_template

    ImportError: No module named flask

    anyone know why this is happening?

  • Shire

    Can someone explain $ python routes.py[/python] a little better?

    • Lalith Polepeddi
      Author

      Looks like that’s a formatting error in the article. Thanks for catching it. Try running $ python routes.py.

      • Shire

        I get this error

        from flask import Flask, render_template

        ImportError: No module named flask

      • Injunire

        You must install flask either in the virtualenv setup at the start of the tutorial, or install it systemwide

  • Peter

    argh! PLEASE DO NOT RECOMMEND USING sudo! It will install things system wide and break other system applications!

    • Dave

      What? Sudo just means the command is run by root, whether it’s installed system wide is dependant on the installer. Lalith mentioned in the article how to set up sandboxed environments for python to deal with the exact problem you’re talking about

    • http://www.binaryexistence.co.uk/ Zenettii

      This is the down side to the internet. Misinformation. sudo does nothing more than escalate privileges..

      Great Tutorial, having spent recent months learning Python as my first language I look forward to testing this out as I’d love to build a python driven website.

  • Daniel Schüler

    Hi there Lalith,

    Great to see that you’re also writing about all sorts of tech-stuff.
    This is Daniel from http://www.fivefreeapps.com.
    I was wondering whether I could write a guest post for you.
    Hope to hear from you soon!

    Regards,
    Daniel

  • Hudson

    I am on the same page as shire:

    Can someone explain $ python routes.py[/python] a little better?

    I am getting the error –

    python: can’t open file ‘routes.py’: [Errno 2] No such file or directory

    like it can’t find it? Is that because I am looking the root folder not my app folder? How can I change this?

    • Lalith Polepeddi
      Author

      Make sure that you’re inside the app/ folder or else the command won’t be able to find routes.py.
      $ cd app/
      $ python routes.py

      • Hudson

        Ok cool, got it… still having the same issues as Shire:

        Now I’m getting the error:
        ImportError: No module named flask

        Any thoughts?

      • Lalith Polepeddi
        Author

        This error message means that Flask isn’t installed. You’ll want to install Flask inside your development environment. Check out the ‘Install Flask’ section above for how to do this.

      • http://twitter.com/masrosid masrosid

        I do have the same error message, but then I realize that I haven’t activate the virtualenv before. So I activate virtualenv, and it works. :)

  • asdasdadasdadsa

    echo "hello world;"

    • RayZ

      echo “Hello World!”;
      :)

  • Peng Lanston

    What’s difference(or say shining point) among other famous framework?

  • E

    Great Article. We would love though if you do one more complex example (database-driven for ex.). Please keep python-related ( and python frameworks) tutorials coming. will subscribe :)

    • Lalith Polepeddi
      Author

      Thanks. I have a follow up article in the pipeline about how to implement an authentication system for the Flask app started in this article and it relies on MySQL.

      • http://www.binaryexistence.co.uk/ Zenettii

        That sounds great Lalith, any chance it will extend to also supporting OpenID authentication (Disqus/Google etc)

      • Lalith Polepeddi
        Author

        That’s a great idea. The article scope might get too big if I try to include OpenID authentication in with rolling your own authentication system, so I’ll see if I can write a separate article on it. In the meantime, there is a Flask extension called Flask-OpenID that adds OpenID authentication support for Flask, and there is an example app here. There is also a brief overview of how to use Flask-OpenID here.

      • PM

        I very much look forward to your followup article! Out of curiosity, will you be using the flask-login extension in your article?

      • Lalith Polepeddi
        Author

        I’ll show you how to roll your own simple authentication system so that when you use extensions like Flask-Login, they will hopefully seem less like black boxes and be easier to adapt for your needs.

      • PM

        That’s even better than I had hoped for! May I be so bold as to ask when this “might” be ready?

      • Lalith Polepeddi
        Author

        It’s finished and submitted, just waiting on Nettuts+ to publish it.

      • PM

        Any word on when the article might get published?

      • Lalith Polepeddi
        Author

        No exact date yet, but it is in the queue. The second part of this series got published here. Authentication is the third part. I’ll reply to this thread when Nettuts+ publishes it.

  • http://www.facebook.com/saurav.1verma Saurav Verma

    awesome tutorial

  • http://www.facebook.com/yashodhan.kulkarni Yashodhan Sunilrao Kulkarni

    Full-stack web application framework that uses python/mysql on the server side and a tightly integrated client side library. Primarily built for erpnext (Open Source ERP)

    https://github.com/webnotes/wnframework

  • http://sahil.me/ Sahil

    Could you please fix all the [arguably minor, but definitely troublesome for beginners] errors in this article?

    Here’s a list:

    1. Code for app/templates/home.html: h1 should be h2, and h2 should be h3 (as per what the subsequent code does)

    2. Just above the first screenshot: the command should be `python routes.py` without the extra [/python] at the end

    3. Code for app/templates/layout.html (after the CSS): the tag is surrounded by strong for some strange reason

    4. Code for app/templates/about.html: Opening tag is written as <h> instead of <h2>

    5. Code for routes.py (just after the above code): wild strong tag appears! (again)

    6. Code for app/templates/layout.html (in the Adding Navigation section): wild strong tag appears! (again)

    Thanks for the useful tutorial!

  • http://www.facebook.com/pixel67 Tony Brown

    what have you done to me? Now I am addicted to python along with JavaScript and Ruby! Oh well I guess I have to `cowboy up` and deal with it lol, thanks again for turning me on to python and flask =] things could be worse lol

  • http://twitter.com/ChidumagaOrji Chidumaga

    Great tut !
    With Windows is it necessary to perform an equivalent of the .bin/activate ?
    Doesn’t the cd command activate the development env. already ?

  • http://twitter.com/ChidumagaOrji Chidumaga

    Just found out that for Windows you can run

    –flaskappScriptsactivate

    to activate a virtenv . You can also simply run –deactivate– to deactivate

  • http://twitter.com/xypotion Max W

    Excellent tutorial! Thanks! Can’t wait to do the next Flask one. :)

  • sudarsan

    The best tutorial ever which provided me a clear understanding of flask. I would like to know whether there is an example using javascript files too.

    • Lalith Polepeddi
      Author

      Including JavaScript files is similar to including CSS files.

      In the “Creating a Home Page” section above, we first created the stylesheet main.css within the static/css/ folder, and then included it in layout.html by writing .

      The same process applies to JavaScript files. Create a new file named main.js within the static/js/ folder, and then include that in layout.html by writing .

      • sudarsan

        Thanks a lot for the clarification. I would like to know how to send an list from flask to javascript. I am able to display it in html. The python function for @app.route has return render_template(‘home.html’,idlist=idlist) as return statement. I tried this link http://stackoverflow.com/questions/11178426/how-can-i-pass-data-from-python-flask-framework-to-javascript but i get undefined error.

      • Lalith Polepeddi
        Author

        If you’re trying to send idlist from Flask to js/main.js, it won’t work because js/main.js is a JavaScript file and not a Jinja template. Try placing the contents of js/main.js inside a tag in your HTML template and then pass it {{ idlist }} as the Stack Overflow example shows.