I’m creating an interface for my team to make API requests. The current process is:
- Enter your search parameters
- Press “Run”
- See results below.
If the search parameter includes more than one item, there is an option box for them to filter the results by search term. This filter is then used to create a map. Everything works, until a user selects something from the option box, and I get the following error:
NameError: name ‘df’ is not defined
What I think is happening is StreamLit is doing what it is supposted to be doing, and rerunning everything on the screen, with the exception of the API call, and is “losing” the df. I’ve cheated by saving the df as a csv, and reloading it, however I would like to learn how to avoid doing this. The code I’m working with is here:
import requests
import pandas as pd
import json
import streamlit as st
from datetime import datetime
st.title('Search')
def query_api(icao, start, end, save_as=f"{datetime.now()}.csv"):
url = "api url"
querystring = {"start":f"{start}","end":f"{end}","icao_address":f"{icao}"}
payload = ""
headers = {"Authorization": ""}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
data = []
for line in response.iter_lines(decode_unicode=True):
if line and '"target":{' in line:
target = json.loads(line)["target"]
data.append(target)
df = pd.DataFrame.from_dict(data)
#Cheat Save
df.to_csv(f'{save_as}.csv')
#Not even sure this return is necessary
return df
with st.form("api Search"):
searchterms = st.text_input("Enter ICAO Address(es), Seperate Each ICAO with a comma.")
start_date = st.date_input("Start Date for your query.")
start_time = st.time_input("Start Time for your query. Default is 00:00:00.")
end_date = st.date_input("End Date for your query")
end_time = st.time_input("End Time for your query. Default is 23:59:59.")
save_as = st.text_input("Enter a filename to save the data to. Default is the current Date/Time.csv")
submitted = st.form_submit_button('Run')
if submitted:
st.spinner(text="Running Query...")
start = str(start_date)+"T"+str(start_time)+".000Z"
end = str(end_date)+"T"+str(end_time)+".000Z"
df = query_api(searchterms, start, end, save_as)
st.success("Your query has finished.")
#creates a list for the user to filter their results by
searchterms = searchterms.split(',')
option = st.selectbox("Select searchterms", searchterms)
#Cheat DF load
df = pd.read_csv(f'{save_as}.csv')
st.dataframe(df[df['id'] == option])
map_data=df[df['id'] == option]
st.map(map_data)
I’ve tagged “cache” and “session state” because I believe this is the route I need to go, however I can’t quite figure it out. Thank you for any help you can provide.