Flask and Python 3 on Glitch… in a couple of lines

I definitively love what glitch.com is doing and it’s just great

  • to use html and javascript at ease and watching your site/blog/wpa come to life while you are writing the code in the browser
  • to remix yours or others projects
  • to import and export projects with github
  • ecc.

To know that you can use also Python is even a nicer surprise. I found also that you can use Python 3 and that you can do it with just 3 files with very little effort. Let’s see how to do it in the fastes and easiest way, in this 3 steps guide. For more advanced way to add pages on your flask web app, go check the links at the bottom of this page, dedicated to flask on glitch.

If you want to know the fastest way to use python to make a web application, create a project on Glitch.com and then create this files:

  • glitch.json
  • requirements.txt
  • server.py


server.py

In this file we just need to import Flask (that is installed with requirements.txt and glitch.json), create the istance of Flask named app and then create the main page in the function hello (with the decorator route and the / that is for the main page) that returns just the words Hello World (to add entire pages look at the posts at the end of this one). To start the server we use the method run() of the istance at the end.

from flask import Flask
app = Flask(__name__)

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

app.run()

requirements.txt

Here we got just what we need to use python and Flask

Flask

glitch.json

Into this file there is what we need to start the site; pratically the start.sh will run the server.py file with python 3, install the modules in the requirements.txt file that we saw in the previous paragraph above:

{
  "install": "pip3 install --user -r requirements.txt",
  "start": "PYTHONUNBUFFERED=true python3 server.py",
  "watch": {
    "ignore": [
      "\\.pyc$"
    ],
    "install": {
      "include": [
        "^requirements\\.txt$"
      ]
    },
    "restart": {
      "include": [
        "\\.py$",
        "^start\\.sh"
      ]
    },
    "throttle": 1000
  }
}

And you’re done. You will have your site with Hello World on it. You can now use your favourite language to build your site or progressive web app from skratch.

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.