Codementor Events

Flask Dashboard Argon - deploy on PythonAnywhere

Published Jun 18, 2019Last updated Feb 26, 2021
Flask Dashboard Argon - deploy on PythonAnywhere

Hello Coder,

I'm writing this article based on my first experience deploying an app on PythonAnywhere platform. Until now I was using my own VPS to release Flask apps into the wild. Here is what I've learned.

Account features

PythonAnywhere offers a free plan, quite limited sometimes, but overall ok, for a learning or demo project (like this one). Using a free plan you will get:

  • Two consoles. I've chosen bash and python 3.6 to prepare the app environment and install the dependencies.
  • The Https certificate, signed by COMODO
  • HTTP to HTTPS redirect

Setup the app

By default, the platform create an initial app starter __flask_app.py template. Note: This file should be edited or replaced with your own bootstrapper. Because my app is published on Github, the first step was to clone the sources and after install the dependencies and create the database.

$ git clone https://github.com/app-generator/flask-argon-dashboard.git
$ pip install -r requirements.txt
$ flask shell
$ >>> from app import db
$ >>> db.create_all() 

At this point, the app has the environment up & running, and the database created. To activate the deployment, we need to manually trigger the deployment:

Flask Dashboard Argon - Deployment on PythonAnywhere

First Run

The first deploy was successfull, and the app is running as expected, without errors:

Flask Dashboard Argon - PythonAnywhere Deployment, First run.

Adding the sitemap.xml

To enable the Google indexing, a few files must be added to the subdomain:

  • sitemap.xml
  • google verification file

To solve this problem, I've updated the views.py (app routing definitions) to handle the sitemap request:

import os
from flask import send_from_directory

# handle the new route
@app.route('/sitemap.xml')
def sitemap():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'sitemap.xml')

This handle is fairly simple, Flask will return the content of sitemap.xml, saved in the root of static directory app / static / sitemap.xml.

Flask Dashboard Argon - PythonAnywhere Deployment, tesing the sitemap

Resources

Thank you for reading!

Discover and read more posts from Adi Chirilov - Sm0ke
get started
post commentsBe the first to share your opinion
Show more replies