Date filter on Apple Health Data - repr_html_()


Hi, I’m trying to build a web app that will parse and visualize heart rate data extracted from Apple Health.

I want to add in a slider where user can choose the date(s) to filter the chosen dates they would like to see their heart rate.

Steps to reproduce

Code snippet:

st. subheader('Upload Data')
file = st.file_uploader("Upload your Apple Health data file (.xml) here...", type=['xml'])

if file is not None:
   tree = ET.parse(file)
   root = tree.getroot()

   attribute = []
   startDate = []
   endDate = []
   value = []
   sourceName = []

   for element in root.xpath("//Record[(@type='HKQuantityTypeIdentifierHeartRate') and \
   contains(@sourceName, 'Watch')]"):
   heartrate = pd.DataFrame({'attribute': attribute,'startDate': startDate, 'endDate': endDate, 'value':value})

   heartrate.value = pd.to_numeric(heartrate.value)
   heartrate.startDate = pd.to_datetime(heartrate.startDate)
   heartrate.endDate = pd.to_datetime(heartrate.endDate).dt.time

   heartrate_new = heartrate.rename (columns={'value': 'BPM', 'startDate': 'Date','endDate': 'Time'})

def df_filter(message,df):
   dates = df['Date'].unique().tolist()
   dates_selection = st.slider('%s' % (message),
                               min_value = min(df['Date']),
                               max_value = max(df['Date']),
                               value =(min(df['Date']),max(df['Date'])))
   mask = df['Date'].between(*dates_selection)
   number_of_result = df[mask].shape[0]
   filtered_df = st.markdown(f'*Available Results: {number_of_result}*')
   return filtered_df

if __name__ == '__main__':
   df = pd.DataFrame(heartrate_new)

   st.title('Datetime Filter')
   filtered_df = df_filter('Move sliders to filter dataframe',df)

   st.title('Data Frame')

   st.markdown('Filtered Data Frame',filtered_df)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

I would like to showcase a table (dataframe) after the date filter is applied

Actual behavior:

I would very much appreciate your help on solving this issue!

In this case, the error appears to be this line:

   filtered_df = st.markdown(f'*Available Results: {number_of_result}*')

st.markdown does not return a dataframe, and so filtered_df ends up being not being a dataframe, so doing st.write(filtered_df) won’t work.

I think you probably meant to do

   filtered_df = df[mask]

Wow! Thank you so much for your help - it works!
Just one more thing, if i may ask. I now encounter the below error:

if __name__ == '__main__':
   df = pd.DataFrame(heartrate_new)
   df.sort_values(by='Date', inplace = True)

   st.header('Datetime Filter')
   filtered_df = df_filter('Move sliders to filter dataframe',df)

   st.header('Data Frame')

   st.markdown('Filtered Data Frame',filtered_df)

I guess its because of this line : df = pd.DataFrame(heartrate_new)
But now quite sure, can you share your thoughts on this?

No, the error message says it is this line

st.markdown('Filtered Data Frame',filtered_df)

The second argument to st.markdown() should be a boolean, not a dataframe.

1 Like

Thank you Goyo!! That was very helpful!

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