Data Visualization adopting Deta (Database) to create Echarts

Good morning to you all,

I am creating a tool using streamlit to measure carbon emissions and i will want your input on how to utilise DETA database to create Echarts and other visualisation graphs.

  1. I have been able to store the data from the tool into a database in deta but after i have retrieved the data into a dataframe and try to use operations such df = df[“scope”].unique to select the unique scope category, it does not work.

  2. I will want to use the data to create a summarised metrics and visualise the results in a racing bar chart using echarts.

Can anyone please help?

I have included a gif file to show the tool AND the code for Data Visualisation section

ezgif.com-optimize
ezgif.com-optimize c1

Hey @sf558,

Thanks for sharing your question! Please update your post to include a properly-formatted code snippet so we can reproduce the issue. Also, can you clarify what you mean by “it does not work”? The more details you can share, the faster we can help you find the solution :slightly_smiling_face:

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)

Hey @sf558,

Are you able to share a runnable code snippet? We won’t be able to run the snippet you shared because we don’t have access to the Excel file referenced and it’s missing your import statements.

Hi Caroline,
please find the link to the repository below. it contains the excel files.

thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.