Python golf: leave a message

simple web app to store messages in a database

I stumbled upon this (python) code-golf question:

Can I code a simple Flask app that gets messages from a web page and stores them in a database?

With Python/Flask it’s actually easier done than said!

The code

from flask import Flask, render_template, request
import sqlite3

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///notes.db'

@app.route('/', methods=['GET', 'POST'])
def index():
    title = "Send a message"
    db = sqlite3.connect('notes.db')
    cursor = db.cursor()

    if request.method == 'POST':
        message = request.form['content']
        cursor.execute('INSERT INTO notes (message) VALUES (?)', (message,))
        db.commit()
        db.close()
        return render_template("message-sent.html.j2", title=title)

    cursor.execute('SELECT COUNT(*) FROM notes')
    count = cursor.fetchone()[0]
    db.close()

    return render_template("index.html.j2", count=count, title=title)

if __name__ == "__main__":
    app.run(debug=True)

Just 775 characters in twenty-seven lines of code. Now let’s try to explain what it does:

The explanation

This code sets up a Flask web application with an SQLite database called notes.db. The application has only one route, the index route ‘/’. When a GET request is made to the ‘/’ page, it connects to the notes.db database and fetches the total number of messages in the database. It then displays the fetched message count using a Jinja2 template called index.html.j2.

When a POST request is made to the ‘/’ route, it connects to the notes.db database, inserts the submitted message into the ’notes’ table, and redirects the user to a success message page using the message-sent.html.j2 Jinja2 template.

604 characters! This Python code is actually almost as concise as the plain English description of what it does.

Related