简体   繁体   中英

What is the most elegant way to check a large combination of inputs?

I have a webapp that displays 11 checkboxes. When checked, the application returns a graph of the data that corresponds to the selected box.

Because the end user can select any variation of these boxes and should expect to see only the graph of those selected, every possible combination must be declared.

Currently, my implementation is as follows. It is quite unsophisticated and will likely take hours, so you can see why I'm looking for something a bit more.. supple?

Discretionary = st.sidebar.checkbox(label = 'Consumer Discretionary', value = False)
Consumer_Staples = st.sidebar.checkbox(label = 'Consumer Staples', value = False)
Energy = st.sidebar.checkbox(label = 'Energy', value = False)
Financials = st.sidebar.checkbox(label = 'Financials', value = False)
Healthcare = st.sidebar.checkbox(label = 'Healthcare', value = False)
Industrials = st.sidebar.checkbox(label = 'Industrials', value = False)
Materials = st.sidebar.checkbox(label = 'Materials', value = False)
Real_Estate = st.sidebar.checkbox(label = 'Real Estate', value = False)
Technology = st.sidebar.checkbox(label = 'Technology', value = False)
Utilities = st.sidebar.checkbox(label = 'Utilities', value = False)


Placeholder = st.empty()

for a in range(0, 500):
    try:
        Data = pd.read_csv('Mydata')
        Data = Data.set_index('Time')
    except:
        pass    
    
    if Communications == True and Discretionary == True:
    
        with Placeholder.container():             
            st.line_chart(data = Data[['Communications','CD Consumer Discretionary']] * 100)
            
    elif Communications == True and Discretionary == False:
    
    
        with Placeholder.container():     
            st.line_chart(data = Data['Communications'] * 100)
            
    elif Discretionary == True and Communications == False:
    
        with Placeholder.container():
            st.line_chart(data = Data['CD Consumer Discretionary'] * 100)```

This problem isn't about permutations / combinations (no need for that), but about filtering .

I'll take the following simplified dataframe as an example for the rest of this post.

import pandas as pd
import streamlit as st

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})

First thing you can do to simplify things, is regroup your checkboxes in a dictionary. It saves you from writing 20 lines of code if you have 20 columns, and makes it easier to adapt to new columns later on

checkboxes = {
    col_name: st.sidebar.checkbox(label=col_name, value=False)
    for col_name in df.columns
}

You can then define a function that filters the data, keeping only columns which checkbox is checked:

def filter_columns(df: pd.DataFrame, checkboxes: dict[str, bool]):
    selected_cols = [col for col, sel in checkboxes.items() if sel]
    return df[selected_cols]

Then filter your data and display it:

df_filtered = filter_columns(df, checkboxes)
st.dataframe(df_filtered)

If you copy all the codeblocks of this example, you should have a functioning streamlit app. You now just have to adapt the dataset to your particular needs.

Once you've filtered your data, create your graphs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM