0

I am New in pandas and streamlit , What I am trying is to filter such a dataframe using streamlit selectbox but unfortunately everything is going well except that when changing the filter value it does not reflect on the shown table

enter image description here as you could see the name in the filter does not update the table

here is the code I have used:

import xlrd
import pandas as pd
import os
from datetime import datetime
import streamlit as st
# import plotly_express as px

# to refer to the file
# change the current directory
currentDir = os.chdir('C:\\Users\\user\\Desktop\\lists');

files=os.listdir(currentDir)
columns=['Name','status','memorize-from-surah','memorize-from-ayah','memorize-to-surah','memorize-to-ayah','memorization-grade','words-meaning-grade','revision-from-surah','revision-from-ayah','revision-to-surah','revision-to-ayah','revision-grade']


folderDF=pd.DataFrame()
for file in files:

    # get the file name without extension for the sheikh name
    sheikh=os.path.splitext(file)[0]
    sheetDF=pd.DataFrame()

    workbook = pd.ExcelFile(f'C:\\users\\user\\Desktop\\lists\\{file}')

    sheets_numbers = len(workbook.sheet_names)
    print(sheets_numbers)
    for i in range(1, sheets_numbers-1):
        # print(workbook.sheet_by_index(i).name)
        current_sheet = pd.read_excel(file,sheet_name=i,header=None,index_col=1)
        date= current_sheet.iloc[6, 10]
        # for j in range(7,current_sheet.nrows):
        #     if current_sheet.cell(j,3).value=="غاب" or current_sheet.cell(j,3).value=="عذر":
        #        for k in range(4,current_sheet.ncols):
        #            current_sheet.cell(j,k).value=""




        sheets=pd.read_excel(file,sheet_name=i,skiprows=11,header=None,index_col=1)


        # df = pd.DataFrame(sheets.iloc[:,1:], index=index)
        #remove the first col
        df=pd.DataFrame(sheets.iloc[:,1:])

        #remove empty rows
        df=df[df.iloc[:,0].notna()]
        #rename the columns

        df.columns = columns

        #get the nrows
        nrows= len(df.index)
        sheikhCol=pd.Series(nrows*sheikh)
        dateCol=pd.Series(nrows*date)
        halkaCol=pd.Series(nrows*i)

        # df.insert(1,"sheikh",sheikhCol)
        df.insert(1,"halka",halkaCol)
        df.insert(2,"sheikh",sheikhCol)
        df.insert(3,"date",dateCol)
        df["sheikh"]=sheikh
        df['date']=date
        df['halka']=i
        if i == 1:
            sheetDF=pd.DataFrame(df)
            datatoexcel = pd.ExcelWriter('C:\\users\\user\\Desktop\\dataOut.xlsx')
            sheetDF.to_excel(datatoexcel)
            datatoexcel.save()

        else:
            sheetDF = pd.concat([sheetDF, df], axis=0)

    folderDF=pd.concat([folderDF,sheetDF],axis=0)



    datatoexcel=pd.ExcelWriter('C:\\users\\user\\Desktop\\dataOut.xlsx')
    folderDF.to_excel(datatoexcel)
    datatoexcel.save()

#

# setting up the streamlit page
st.set_page_config(page_title='makraa reports',layout='wide')




# make filteration
#
st.sidebar.header("make filtration criteria")
nameFilter= folderDF['Name'].unique()
halkaFilter= folderDF['halka'].unique()
sheikhFilter= folderDF['sheikh'].unique()


student_choice= st.sidebar.selectbox("select the student Name",nameFilter)
halka_choice= st.sidebar.selectbox("select the halka Number",halkaFilter)
sheikh_choice= st.sidebar.selectbox("select the sheikh Number",sheikhFilter)
# student_choice2= st.sidebar.multiselect("select the student Name",options=nameFilter,default=nameFilter)

# filteredDf=folderDF[folderDF["Name"]== student_choice]
filteredDf = folderDF[(folderDF["Name"] == student_choice) & (folderDF["halka"] == halka_choice)]
# filteredDf=folderDF.query('Name==@student_choice')
st.write(filteredDf)







note st.dataframe(filteredDf) does not make any difference

the streamlit version I used is 0.75 , since the recent version gave me the StreamlitAPIException like that enter link description here

could you give a hand in this

aboarab
  • 45
  • 5

1 Answers1

0

Here is a sample code with example data.

Code

import streamlit as st
import pandas as pd


data = {
    'Name': ['a', 'b', 'c'],
    'halka': [1, 2, 3]
}


st.set_page_config(page_title='makraa reports',layout='wide')

folderDF = pd.DataFrame(data)


# make filteration
#
st.sidebar.header("make filtration criteria")
nameFilter = folderDF['Name'].unique()
halkaFilter = folderDF['halka'].unique()
# sheikhFilter = folderDF['sheikh'].unique()


student_choice = st.sidebar.selectbox("select the student Name", nameFilter)
halka_choice = st.sidebar.selectbox("select the halka Number", halkaFilter)
# sheikh_choice= st.sidebar.selectbox("select the sheikh Number",sheikhFilter)
# student_choice2= st.sidebar.multiselect("select the student Name",options=nameFilter,default=nameFilter)

filteredDf = folderDF[(folderDF["Name"] == student_choice) & (folderDF["halka"] == halka_choice)]
# filteredDf = filteredDf[filteredDf["halka"] == halkaFilter]

st.write(filteredDf)

Output

enter image description here

ferdy
  • 4,396
  • 2
  • 4
  • 16
  • still not working ,here is the complete code , I read the dataframe form an excel , I edit it in the main post – aboarab May 19 '22 at 05:05