Hello,
First time here and hoping someone can help. I have a portfolio web app to showcase some SQL queries I have written that I want to put into streamlit. My goal is to have the viewer copy any of the aforementioned queries from a column (col1), paste them into a text area to submit in another column (col2) and display the output in a dataframe/table format in a wider third column (col3). The results I currently return are two columns using iterate but would like to just pass any/all columns that are part of the query. I tried a number of other code pieces (pandas_gbq.read_gbq, read_sql_query, read_sql_table) in place of iterate but can’t seem to find one that transitions the complete query results after sql_executor does its part into a dataframe that I can use. I’ve also searched multiple wording of what I’m trying to do with no answers that I could see.
Any help would be appreciated. Here is my code:
from google.cloud.bigquery import dbapi
import pandas as pd
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = '*.json'
#DB management
st.set_page_config(layout="wide")
client = bigquery.Client()
conn = dbapi.Connection(client)
c = conn.cursor()
def sql_executor(raw_code):
c.execute(raw_code)
data = c.fetchall()
return data
def main():
menu = ["SQL Queries"]
with st.sidebar:
choice = st.selectbox("Menu",menu)
if choice == "SQL Queries":
st.subheader("SQL Queries")
with st.container():
query_col1, query_col2 = st.columns(2)
with query_col1:
st.text("Click the copy button for one of the SQL queries below")
st.code("SELECT * FROM hosp_info.hospital_general_information")
with query_col2:
with st.form(key='query_form'):
raw_code = st.text_area("Paste SQL query here")
submit_code = st.form_submit_button("Execute")
with st.container():
query_col3, query_col4 = st.columns(2)
with query_col3:
if submit_code:
st.code(raw_code)
query_results = sql_executor(raw_code) # Results
for row in query_results:
st.write(f'{row.provider_id} {row.hospital_name}')
if __name__ == '__main__':
main()
type or paste code here
Thanks!