Skip to content

Instantly share code, notes, and snippets.

@GeorgePearse
Created August 21, 2022 11:46
Show Gist options
  • Select an option

  • Save GeorgePearse/dd63d46e4ac9a4bfeff8dfa75411b464 to your computer and use it in GitHub Desktop.

Select an option

Save GeorgePearse/dd63d46e4ac9a4bfeff8dfa75411b464 to your computer and use it in GitHub Desktop.
from fastapi import FastAPI, Path
from utils import create_connection, sql_to_df
import pandas as pd
app = FastAPI()
@app.get("/query/{query_name}")
async def root(
query_name: str = Path(
title="Query name of the query to run",
default='hospital_counts'),
json_format: str = Path(
title='Format of returned json',
default='index')
):
analysis_conn = create_connection("core.db")
queries_conn = create_connection("queries.db")
selected_query = list(queries_conn.execute(f"""
select query_contents from queries where query_name = '{query_name}'
""").fetchall())[0]
selected_query = selected_query[0]
results_df = sql_to_df(analysis_conn, selected_query)
return results_df.to_json(orient=json_format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment