Flask SEO Tutorial

As we bring this initial installment of Flask tutorials to a close, I thought it would be helpful to discuss SEO, or "search engine optimization." SEO speaks to the methodologies used to "optimize" your website with search engines in mind, usually with the goal to improve your search ranking.

A Google search for SEO reveals a vast array of information, and one might wonder where to even begin! I have some good news for you though! SEO is actually very easy. Today's search engines are very smart. The main search engine people care about is Google, though you shouldn't forget about the others, especially when considering your website specifically.

For example, PythonProgramming.net's users are programmers. A sweeping judgement of programmers says that many of them are concerned with both security and privacy, since they go hand-in-hand. This means, many of them might actually not be using the more popular search engine of Google.com, they might actually opt to use DuckDuckGo, which is a search engine that does not track its users.

Sure enough, a substantial portion of my users are coming from DuckDuckGo. Another rising source of search traffic is Bing.com

Search traffic is often referred to as "organic" traffic, since this is traffic that is not direct or referral traffic. It is just traffic that happens to naturally find you through search.

You have to think about what kind of website you run, and what your content is. If you are a heavy-content website, like PythonProgramming.net is, chances are, a lot of your traffic will come from search engines, likely more than 50% of your traffic.

If your website is more of a simple web-application, without much content, you might get a lot of traffic from a specific keyword, but maybe more of your traffic will wind up actually being direct.

Most people, however, have correlated website growth over time with organic growth. I can definitely attest to this, so then the next question is always "how can I boost my organic results."

The first rule to SEO is: You cannot fake it.

A decade ago, you could, and it was big business. If you search for SEO right now, you will find people still claiming that meta tags with meta descriptions carry weight. People actually still debate this matter, but it's really not up for debate at all, Google has explicitly said meta descriptions are only used in the search results, as the text underneath your link, and this only happens if they don't already have one that their algo thinks is better.

Backlinks are also held very highly. This is for good reason, but people also engage in the buying and selling of backlinks, usually involving spam bots that go around just spamming various links. Again, in the past, Google's algorithm for valuing back links was super simple, and weighted heavily based on the number of backlinks. This was a decade ago, however.

Okay, then what works?

High quality, engaging, and timely content. That's it! I can see it now:

"Wait, so you're telling me that all I need to do is have a quality website?"

Yes, that is what I am telling you.

Google and Bing both have a massive stake in not being fooled by tricks and schemes. Their job is to serve you with the best content to match you specifically. That's what they've hired some of the best developers and engineers out there to do. If you think spamming a blog with links is going to fool them, you're only fooling yourself!

Sometimes, you can speed the process of getting "indexed" up. This is most easily done by submitting a sitemap to Google and Bing. Depending on your website, how you make a sitemap will vary. We make one with some simple Flask code:

Function within the file: __init__.py
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
      """Generate sitemap.xml. Makes a list of urls and date modified."""
      ten_days_ago=(datetime.now() - timedelta(days=7)).date().isoformat()
      # static pages
      for rule in app.url_map.iter_rules():
          if "GET" in rule.methods and len(rule.arguments)==0:

      sitemap_xml = render_template('sitemap_template.xml', pages=pages)
      response= make_response(sitemap_xml)
      response.headers["Content-Type"] = "application/xml"    
      return response
    except Exception as e:

That function will generate a sitemap for you. I will stress again, however, that you actually do not even need a sitemap, nor do you need to submit one to the search engines. They will naturally search for one, but they also will simply go through all your pages. This means, of course, that you will want to make sure all your pages are indeed connected. There should always be good navigation on your website. Again, this just goes along with having a quality website, and being very user-friendly.

To expedite things, however, you can submit your sitemap to Google and Bing, asking them to index and crawl your pages. You can do this for all the search engines you want. Most of them will have an option for you to tell them to crawl it right now, though they generally have a daily or monthly limit on the number of times you can do this.

For Google Webmasters, head here: Google Webmasters for Submitting Sitemap and tracking site issues.

Google webmasters can also notify you about various crawling errors, site bugs, 404s, and more. Definitely worth signing up to. Bing also has a webmasters page.

So, the most important thing to your website's ranking is user experience.

Good content is important, definitely the crux of what matters most, but also your website's performance matters greatly. If you remember the days of dialup, then you might have a little sympathy for websites that take a few seconds to load. The average user of today, however, has absolutely no patience for anything past 1 second loading time. Most users, while maybe not realizing it, begin to feel slightly frustrated with delays beyond 500ms (half of a second).

A very useful website comes from Google Developers, called Web Fundamentals.

This will help with many aspects related to your user's experience.

In the next tutorial, we're going to be discussing the Flask Includes functionality.

  • Introduction to Practical Flask
  • Basic Flask Website tutorial
  • Flask with Bootstrap and Jinja Templating
  • Starting our Website home page with Flask Tutorial
  • Improving the Home Page Flask Tutorial
  • Finishing the Home Page Flask Tutorial
  • Dynamic User Dashboard Flask Tutorial
  • Content Management Beginnings Flask Tutorial
  • Error Handling with Flask Tutorial
  • Flask Flash function Tutorial
  • Users with Flask intro Tutorial
  • Handling POST and GET Requests with Flask Tutorial
  • Creating MySQL database and table Flask Tutorial
  • Connecting to MySQL database with MySQLdb Flask Tutorial
  • User Registration Form Flask Tutorial
  • Flask Registration Code Tutorial
  • Finishing User Registration Flask Tutorial
  • Password Hashing with Flask Tutorial
  • Flask User Login System Tutorial
  • Decorators - Login_Required pages Flask Tutorial
  • Dynamic user-based content Flask Tutorial
  • More on Content Management Flask Tutorial
  • Flask CMS Concluded Flask Tutorial
  • The Crontab Flask Tutorial
  • Flask SEO Tutorial
  • Flask Includes Tutorial
  • Jinja Templating Tutorial
  • Flask URL Converters Tutorial
  • Flask-Mail Tutorial for email with Flask
  • Return Files with Flask send_file Tutorial
  • Protected Directories with Flask Tutorial
  • jQuery with Flask Tutorial
  • Pygal SVG graphs with Flask Tutorial
  • PayPal with Flask Web Development Tutorial
  • Securing your Flask website with SSL for HTTPS using Lets Encrypt