Sharing with the internet

Setup

python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
mkdir c6
cd c6
wget https://raw.githubusercontent.com/writeson/the-well-grounded-python-developer/integration/examples/CH_06/requirements.txt
pip install -r requirements.txt

It is also straightforward with httpie package, i.e.,

pip install httpie

The environment can be activated as follows,

. ~/myapp/venv/bin/activate

The files, https://github.com/cambridge-ceu/CEU-scientific-meetings/tree/gh-pages/c6, are organised as follows,

c6
├── app.py
├── requirements.txt
├── static
│   ├── Caprion
│   ├── css
│   │   ├── index.css
│   │   └── myblog.css
│   └── js
│       └── index.js
└── templates
    ├── base.html
    └── index.html

where and our project directory Caprion/ is to be access from http://localhost:#/static/Caprion with # to be 5000 (Flask default) or 8000 (gunicorn default).

Running

Our call is gunicorn -c gunicorn_config.py app:app, where gunicorn_config.py can be seen as a python program with only assignment statements.

One might as well create a configuration file such as gunicorn_config.py (required to have a .py extension)

# bind = "127.0.0.1:8000"
bind = "unix:/home/jhz22/web.sock"
workers = 2
loglevel = "info"
errorlog = "/public/home/jhz22/logs/gunicorn_error.log"
accesslog = "/public/home/jhz22/logs/gunicorn_access.log"

Moreover, one can use uvicorn following pip install uvicorn[standard].

uvicorn app:app --host 127.0.0.1 --port 8000 --workers 2

Queries

We can also apply flask routes, which gives,

Endpoint     Methods  Rule
-----------  -------  -----------------------
home         GET      /
static       GET      /static/<path:filename>
static_file  GET      /<path:path>

Furthermore, we could use curl http://localhost:8000/, curl http://127.0.0.1:8000/static/Caprion/A1BG/results?analysis==null in a familiar REST API style and/or pipe into jq to prettify.

We can use http http://localhost:8000/ with output,

HTTP/1.1 200 OK
Connection: close
Content-Length: 1223
Content-Type: text/html; charset=utf-8
Date: Mon, 27 Nov 2023 11:16:25 GMT
Server: gunicorn

<!DOCTYPE html>
<html>
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Your Second Web Server</title>
    <link rel="stylesheet" type="text/css" href="/static/css/myblog.css">
    <link rel="stylesheet" type="text/css" href="/static/css/index.css">
</head>
<body>
    <div id="header">
        <h1>MyBlog Home Page</h1>
    </div>
    <div id="content">
<h2>Current time: 2023-11-27 11:16:25.877095</h2>
<p><a href="http://127.0.0.1:8000/static/Caprion/index.html">Caprion</a></p>
<p>Page visits: 1</p>
<p>List of available banner colors:</p>
<ul>
    <li>aqua</li>
    <li>purple</li>
    <li>olive</li>
    <li>salmon</li>
    <li>lightcoral</li>
</ul>
<div id="color-change">
    <button class="change-banner-color">
        Change Banner Color
    </button>
</div>
    </div>
    <div id="footer">
        <h4>Copyright &copy; 2020 by MyBlog</h4>
    </div>
</body>
<script>
    const banner_colors = ["aqua", "purple", "olive", "salmon", "lightcoral"];
</script>
<script src="/static/js/index.js"></script>
</html>