<jupyter_start><jupyter_text>Flask Demo<jupyter_code>import json
import os
import subprocess
import webbrowser

import psutil
import requests
# remove database file if exists to start fresh
if os.path.isfile("app.db"):
    os.remove("app.db")
# readable output for JSON
def print_readable_json(x: dict, indent: int = 2):
    print(json.dumps(x, indent=indent))
# start and track a uvicorn subprocess
flask_proc = subprocess.Popen(["python", "./app/flask_main.py"])
print("flask host process started with pid: {}".format(flask_proc.pid))
print("flask pid exists: {}".format(psutil.pid_exists(flask_proc.pid)))
target = "http://localhost:5000"
# test hello world
resp = requests.get(target)
print_readable_json(resp.json())
# make an items request
resp = requests.get("{}/items/1".format(target), params={"q": "first item description"})
print_readable_json(resp.json())
# send an improper request through
resp = requests.get(
    "{}/items/first".format(target), params={"q": "first item description"}
)
print_readable_json(resp.json())
# get from the sklean dataset
resp = requests.get("{}/sklearn_wine".format(target))
sklearn_dataset = resp.json()
print_readable_json(sklearn_dataset[:1])
# post records to the database
resp = requests.post("{}/sqlite_wine".format(target), data=json.dumps(sklearn_dataset))
print_readable_json(resp.json())
# read from the database
resp = requests.get("{}/sqlite_wine".format(target), params={"limit": "2"})
results = resp.json()
print_readable_json(results)
# kill the uvicorn subprocess
proc_killed = flask_proc.kill()
print("flask pid exists: {}".format(psutil.pid_exists(flask_proc.pid)))<jupyter_output>flask pid exists: False
{'fraction_non_alphanumeric': 0.14705882352941177, 'fraction_numerical': 0.004801920768307323, 'mean_word_length': 6.789719626168225, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '457558', 'license': 'MIT', 'n_tokens_mistral': 563, 'n_tokens_neox': 542, 'n_words': 131, 'path': 'flask/demo.ipynb', 'repo_name': 'd33bs/api_experiments', 'size': '1666'}