Put our Flask blog on Glitch.com for free

We made our basic blog, but we want it online, right? Why we do not use glitch.com to have it on line for free?

The code in start.sh

python run.py

The code in run.py

from flask import Flask, render_template

app = Flask(__name__, static_folder="static", template_folder="templates")

@app.route("/")
def homepage():
    posts = [
        
        {"title": "Post nr. 1",
        "body": "This is the first post of my blog",
        "author": "Giovanni Pyhton",
        "date": "06/07/2019"},

        {"title": "Post. nr.2",
        "body": "Hello... we're back, with the second post",
        "author": "Giovanni Pyhton",
        "date": "06/07/2019"}
    ]

    author = True
    date = True


    return render_template("homepage.html", posts=posts, author=author, date=date)
  
if __name__ == "__main__":
    app.run()

The text in requirements.txt with the module needed

Flask
gunicorn

The code in templates/template.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
    <body>

    {% block page %}
    {% endblock %}



</body></html>

The code in templates/index.html

{% extends 'template.html' %}

{% block page %}

<h1>Welcome to our Homepage</h1>

{% for post in posts %}
    <h3> {{ post.title }}</h3>
    <p>{{ post.body }}</p>

    {% if author %}
    <span class="span1">{{ post.author }} </span>
    {% endif %}
    
    {% if date %}
    <span class="span1">{{ post.date }}</span>
    {% endif %}
 {% endfor %}

{% endblock %}

The code in static/style.css

/* CSS files add styling rules to your content */

body {
  margin-left: 10%;
  margin-right: 10%;
  font-family: "Benton Sans", "Helvetica Neue", helvetica, arial, sans-serif;
  margin: 2em;
}

h1 {
  bacground-color: coral;
  font-style: italic;
  color: #373fff;
}

So, as you can see, it’s pretty the same code that we created yet for the blog on the local server. You can of course deploy the site in any hosting platform that supports python.

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.