Can't select a list of column names and see the data within those columns in Streamlit

1k views Asked by At

In my code I need to be able to filter column names(on the left component) and show only filtered columns with information after user uploaded a CSV file. This is my code but I am stuck on it at the moment and have no ideas of how I can implement the requirements.

try:
    from enum import Enum
    from io import BytesIO, StringIO
    from typing import Union

    import pandas as pd
    import streamlit as st
except Exception as e:
    print(e)

STYLE = """
<style>
img {
    max-width: 100%;
}
</style>
"""


class FileUpload(object):

    def __init__(self):
        self.fileTypes = ["csv", "png", "jpg"]

    def run(self):
        """
        Upload File on Streamlit Code
        :return:
        """
        st.info(__doc__)
        st.markdown(STYLE, unsafe_allow_html=True)
        st.sidebar.title("Filter data")
        st.set_option('deprecation.showfileUploaderEncoding', False)
        file = st.file_uploader("Upload file", type=self.fileTypes)
        show_file = st.empty()
        if not file:
            show_file.info("Please upload a file of type: " +
                           ", ".join(["csv", "png", "jpg"]))
            return
        content = file.getvalue()
        if isinstance(file, BytesIO):
            show_file.image(file)
        else:
            data = pd.read_csv(file)
            st.dataframe(data.head(10))
        file.close()


if __name__ == "__main__":
    helper = FileUpload()
    helper.run()
1

There are 1 answers

0
drops On

If you read in a csv-file, you can use the multi-select widget to select the columns you want to show:

import streamlit as st
import pandas as pd
  
# load csv
uploaded_file = st.file_uploader('upload')

# into dataframe
df = pd.read_csv(uploaded_file)             

'## multi select'
cols = st.multiselect('select columns:', df.columns, default=[])
st.write('You selected:', cols)

# show dataframe with the selected columns
st.write(df[cols])

In the current version (v0.68.1) the file uploader is buggy. So if you get an error on rerun, consider downgrading to v0.66