Python 3 with Flask on Glitch.com!

Finally we can use Python 3 on Glitch.com with Flask to create app or sites or blogs or whatever you need to be on the web with the beauty of Python, the most elegant language ever.

In the following video you will see me exploring the remixed code to use flask with Python 3 on glitch.com. I talked about glitch.me and flask in many posts, but I always used python 2 for that. Now I found this code and tried it on my sites and I saw all the benefits of this upgrade.

Why?

  • First: I use Python 3, so I am more at ease with it;
  • second: Python 2 will be non longer supported
  • third: it is faster, the changes on the site (even in the html files) are now immediate, while it took some minutes with python 2 (you could not predict when the changes would be applied and if there was something that was not going well, so now it is a pleasure to see immediately the effect of your modifications)

What you need to do it

You need these files:

  • InstallReqsAndStart.sh
  • package.json
  • watch.json
  • server.py

InstallReqsAndStart.sh

#!/bin/bash

MD5=$(md5sum .requirements.txt | cut -f1 -d' ')
if ! [ -d ".data/$MD5-site-packages" ]; then
    rm -rf .data/*-site-packages
    pip3 install -U -r .requirements.txt -t ".data/$MD5-site-packages" 
fi
exec env PYTHONPATH="$PWD/.data/$MD5-site-packages" python3 server.py

package.json

{
  "//1": "describes your app and its dependencies",
  "//2": "https://docs.npmjs.com/files/package.json",
  "//3": "updating this file will download and update your packages",
  "name": "my-glitch-app",
  "version": "0.0.1",
  "description": "What am I about?",
  "scripts": {
    "start": "bash installReqsAndStart.sh"
  },
  "dependencies": {},
  "engines": {
    "node": "9.x"
  },
  "repository": {
    "url": "https://glitch.com/edit/#!/my-glitch-app"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "glitch",
    "express"
  ]
}

requirements.txt

Flask

server.py

This will create just a line with two words (Hello World) in the page. To see how to create a complex site, go in the links at the end of the post.

from flask import Flask
app = Flask(__name__)

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

app.run()

 

The video with the live discovery of Python 3 on Glitch.com

Examples of sites with flask and python3

How to import the code you see in pil.glitch.me

Go in tool in the left bottom of the screen of the glitch page and then, from the menu, click on import from glitch (2 times). On the pop up window insert formazione/flask and you will get the code that you will personalize.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.