quaintitative

I write about my explorations in AI and other quaintitative areas.

For more about me and my other interests, visit playgrd, quaintitative or socials below


Categories
Subscribe

(Almost) The Simplest Server Ever

What good is any script you have written if all you can do is send it round as a .py file or as a Jupyter notebook?

What we really want to do is to be able to deploy that script in some way online for all to access.

The first step on that path would be to learn a web framework. There are a whole range of options, from Express in Javascript to Flask or Django in Python. Flask is probably one of the easiest to pick up. So that’s where we will start.

I wanted to title this as the simplest server ever. But that would mean a server which prints its outputs to the terminal. And that won’t be much fun.

So this is (almost) the simplest server ever, because we will include a simple set of templates, and hence be able to see webpages being served by the server.

I will, in the next related tutorial, then show to integrate some useful functions so we can use the server to serve something useful.

First off, install Flask. If you don’t know how to install packages in Python, do a search on ‘pip install’. So for flask all we need to do in any terminal is do type this in.

pip install flask

Next, we write a Python script, named run.py.

Main script to run server So for the run.py script, we first import the libraries we need - the main Flask class, as well as the render_template function.

from flask import Flask
from flask import render_template

We then instantiate a Flask object.

app = Flask(__name__)

The next two parts are almost identical. What they do is to set a route (which basically means the relative address/path of the webpage). And when the user goes to that page, a webpage is rendered based on a template, and the title and bodytext are passed to the page.

That may be confusing. So let me explain a little bit more. We are basically saying that when the user goes to the domain (e.g. domain.com/), he will see a webpage rendered using the HTML template base.html and the values assigned to title and bodytext will be passed to the template. You will see how we read in title and bodytext later when we do the templates. The parts right at the top @app.route("/") are called decorators. We shall not go into the details on these now, but will cover them another time.

@app.route("/")
def main():
    return render_template('base.html', 
                        title='This is the title', 
                        bodytext='This is the body text')

@app.route("/extended")
def extended():
    return render_template('extension.html', 
                        title_extended='This is the title of another page', 
                        bodytext_extended='This is the body text of another page ')

And finally, we run the app. The first line is just to check that the run.py file is the main file being run (i.e. this file is not being called by another file). We can set the host and port we want to access the webpage from here too.

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True, port=8000)

Templates Now we move to the templates. base.html is the base template, and extension.html extends (or is based on) the base template.

For base.html we basically do the normal HTML stuff, which I have covered before, but with two key differences. We add in stuff between the mustaches {}. Basically what we are doing is to either -

Take a look at the base.html and extension.html templates to understand this part.

And that’s it. These are the only differences between a template and the usual static HTML pages.

And that’s it. Now all you have to do is to type python run.py in your terminal and you will see the webpages being served on -

The code for this tutorial is available here


Articles

Comparing Prompts for Different Large Language Models (Other than ChatGPT)
AI and UIs
Listing NFTs
Extracting and Processing Wikidata datasets
Extracting and Processing Google Trends data
Extracting and Processing Reddit datasets from PushShift
Extracting and Processing GDELT GKG datasets from BigQuery
Some notes relating to Machine Learning
Some notes relating to Python
Using CCapture.js library with p5.js and three.js
Introduction to PoseNet with three.js
Topic Modelling
Three.js Series - Manipulating vertices in three.js
Three.js Series - Music and three.js
Three.js Series - Simple primer on three.js
HTML Scraping 101
(Almost) The Simplest Server Ever
Tweening in p5.js
Logistic Regression Classification in plain ole Javascript
Introduction to Machine Learning Right Inside the Browser
Nature and Math - Particle Swarm Optimisation
Growing a network garden in D3
Data Analytics with Blender
The Nature of Code Ported to Three.js
Primer on Generative Art in Blender
How normal are you? Checking distributional assumptions.
Monte Carlo Simulation of Value at Risk in Python
Measuring Expected Shortfall in Python
Style Transfer X Generative Art
Measuring Market Risk in Python
Simple charts | crossfilter.js and dc.js
d3.js vs. p5.js for visualisation
Portfolio Optimisation with Tensorflow and D3 Dashboard
Setting Up a Data Lab Environment - Part 6
Setting Up a Data Lab Environment - Part 5
Setting Up a Data Lab Environment - Part 4
Setting Up a Data Lab Environment - Part 3
Setting Up a Data Lab Environment - Part 2
Setting Up a Data Lab Environment - Part 1
Generating a Strange Attractor in three.js
(Almost) All the Most Common Machine Learning Algorithms in Javascript
3 Days of Hand Coding Visualisations - Day 3
3 Days of Hand Coding Visualisations - Day 2
3 Days of Hand Coding Visualisations - Day 1
3 Days of Hand Coding Visualisations - Introduction