A Server

Overview

concept map of Flask server
Figure 1: Concept Map

Terms defined: dataframe, environment variable, query parameter, route (in server), URL fragment

Outline

"""Hello server."""

from flask import Flask

HELLO = """\
<html>
<body>
<h1>Hello</h1>
</body>
</html>
"""


app = Flask("hello")


@app.route("/")
def root():
    return HELLO
"""Fetch HTML from URL and display."""

import argparse
import httpx


HOST = "127.0.0.1"
PORT = 5000
RESOURCE = "/"


def main():
    """Main driver."""
    opt = parse_args()
    url = f"http://{opt.host}:{opt.port}{opt.resource}"
    response = httpx.get(url)
    print(response.status_code)
    print(response.text)


def parse_args():
    """Parse command-line arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default=HOST, help="server address")
    parser.add_argument("--port", type=int, default=PORT, help="server port")
    parser.add_argument("--resource", type=str, default=RESOURCE, help="resource path")
    return parser.parse_args()


if __name__ == "__main__":
    main()
def create_app():
    """Build application and configure routes."""
    app = Flask("func")
    CORS(app)

    @app.get("/")
    def root():
        """Display home page as HTML."""
        return util.as_html()

    return app
def create_app():
    """Build application and configure routes."""
    app = Flask("routes")
    CORS(app)

    @app.get("/")
    def root():
        return util.as_html()

    @app.get("/col/<name>")
    def column(name):
        data = util.as_dataframe()
        return jsonify(list(data[name]))

    @app.get("/row/<staff_id>")
    def row(staff_id):
        staff_id = int(staff_id)
        data = util.as_dataframe()
        row = data.filter(pl.col("staff_id") == staff_id).row(0, named=True)
        return jsonify(row)

    return app
def create_app():
    """Build application and configure routes."""
    app = Flask("err")
    CORS(app)

    @app.get("/")
    def root():
        return util.as_html()

    @app.get("/col/<name>")
    def column(name):
        try:
            data = util.as_dataframe()
            return jsonify(list(data[name]))
        except Exception as exc:
            abort(util.HTTP_400_BAD_REQUEST, str(exc))

    @app.get("/row/<staff_id>")
    def row(staff_id):
        try:
            staff_id = int(staff_id)
            data = util.as_dataframe()
            row = data.filter(pl.col("staff_id") == staff_id).row(0, named=True)
            return jsonify(row)
        except Exception as exc:
            abort(util.HTTP_400_BAD_REQUEST, str(exc))

    return app