hey Caroline,
I have attached a link to repository below for reference.
Objective
I am creating a tool to help calculate carbon emission within different scope categories. the user will enter the details of their emission and the tool will calculate and store the information in a database (DETA).
After saving the data, the data visualisation tab will be used to summarise the data within the database (DETA).
Problem
When i try to retrieve the data (SCOPE) from the database into a selectbox in the data visualisation tab (see line 122 of code). i get the following error message:
**tTypeError: 'FetchResponse' object is not iterable**
the data is stored in a string within the database. I would like to use the selectbox to retrieve all the data related to a specific category within the database.
please find a screenshot of the database attached.
Code
st.cache_resource
def get_data ():
path = "Emissions.xlsx"
return pd.read_excel(path,sheet_name="Sheet2",usecols="A:I")
#----remember to remove duplicates
data = get_data()
data_na = data.dropna()
if selected == "Data Entry":
options1 = data_na.iloc[:,0].unique()
selected_option1 = st.selectbox("Select Scope:",options1)
#----filtering scope-------
filtered_data = data_na[data_na.iloc[:,0]==selected_option1]
#----get unique values for option 2-----
option2 = filtered_data.iloc[:,1].unique()
selected_option2 = st.selectbox("Select Category:",option2)
#-----filter based on option 2-----
filter_2 = filtered_data[filtered_data.iloc[:,1]==selected_option2]
option3 = filter_2.iloc[:,2].unique()
selected_option3 = st.selectbox("Select Sub Category:",option3)
#----filter based on option 3----
filter_3 = filter_2[filter_2.iloc[:,2]== selected_option3]
option4 = filter_3.iloc[:,3].unique()
selected_option4 = st.selectbox("Select Material:",option4)
#-----filter based on option 4----
filter_4 = filter_3[filter_3.iloc[:,3]==selected_option4]
option5 = filter_4["UOM"].unique()
selected_option5 = st.selectbox("Select Unit of Measure:",option5)
#----filter based on option 5-------
filter_5 = filter_4[filter_4["UOM"]== selected_option5]
option6 = filter_5["GHG/Unit"].unique()
selected_option6 = st.selectbox("Select Unit:",option6)
#-----filter based on last option-----
filter_6 = filter_5[filter_5["GHG/Unit"]== selected_option6]
option_7 = filter_6["GHG Conversion Factor 2022"].unique()
selected_option7 = st.selectbox("Emission Factor:",option_7)
#option7_int = int(selected_option7)
#----create an input field-------
with st.form("my_form", clear_on_submit=True):
values = st.number_input("Enter Amount",format="%i",min_value=0)
values_int = int(values)
#----multiplying the two columns together to find total emission----
emission = int(selected_option7 * values_int)
total = st.number_input("Total Emissions:",emission)
#---Creating the submit button-------------
submitted = st.form_submit_button("Save Data")
if submitted:
selected_option1 = selected_option1
selected_option2 = selected_option2
selected_option3 = selected_option3
selected_option4 = selected_option4
values = values
total = total
st.success("Data Saved Successfully!")
db.put({"Scope":selected_option1,"Category":selected_option2,"subCategory":selected_option3,"Material":selected_option4,"Quantity":values,"Total Emission":total})
#-------Get the data and plotting the graph---------------
if selected == "Data Visualization":
st.header("Emission Dashboard")
with st.form("Saved_scope"):
scope = st.selectbox("Select Scope:",get_scope())
submitted = st.form_submit_button("Plot Scope")
#df = st.dataframe(db_content)