awacke1 commited on
Commit
c4b88e2
ยท
1 Parent(s): 3c1d080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -35
app.py CHANGED
@@ -11,12 +11,14 @@ def load_jsonl(file_path):
11
  data.append(json.loads(line))
12
  return pd.DataFrame(data)
13
 
14
- # Function to filter DataFrame by keyword
15
- def filter_by_keyword(df, keyword):
16
- return df[df.apply(lambda row: row.astype(str).str.contains(keyword).any(), axis=1)]
 
17
 
18
  # Function to generate HTML with textarea
19
- def generate_html_with_textarea(text_to_speak):
 
20
  return f'''
21
  <!DOCTYPE html>
22
  <html>
@@ -33,7 +35,7 @@ def generate_html_with_textarea(text_to_speak):
33
  <body>
34
  <h1>๐Ÿ”Š Read It Aloud</h1>
35
  <textarea id="textArea" rows="10" cols="80">
36
- {text_to_speak}
37
  </textarea>
38
  <br>
39
  <button onclick="readAloud()">๐Ÿ”Š Read Aloud</button>
@@ -68,7 +70,7 @@ if 'last_clicked_row' not in st.session_state:
68
  st.session_state['last_clicked_row'] = None
69
 
70
 
71
- # Expander UI for common terms
72
  with st.expander("Search by Common Terms ๐Ÿ“š"):
73
  cols = st.columns(4)
74
  for term in top_20_terms:
@@ -77,35 +79,22 @@ with st.expander("Search by Common Terms ๐Ÿ“š"):
77
  filtered_data = filter_by_keyword(data, term)
78
  st.write(f"Filter on '{term}' ๐Ÿ“Š")
79
 
80
- # Display the filtered dataframe in the sidebar with buttons for each row
81
- with st.sidebar:
82
- st.write("Filtered Data:")
83
- st.dataframe(filtered_data)
84
- for idx, row in filtered_data.iterrows():
85
- if st.button(f"Select Row {idx}", key=f"row_{idx}"):
86
- st.session_state['last_clicked_row'] = idx
87
-
88
- # Generate HTML content for the selected row
89
- first_three_columns = row.iloc[:3]
90
- additional_info = ' '.join([f"{col}: {val}" for col, val in first_three_columns.items()])
91
- question_text = row.get("question", "No question field")
92
- full_text = f"{additional_info} Question: {question_text}"
93
-
94
- st.session_state['selected_row_html'] = generate_html_with_textarea(full_text)
95
-
96
- # Check if 'last_clicked_row' is set and in the filtered data
97
- if st.session_state['last_clicked_row'] is not None and st.session_state['last_clicked_row'] in filtered_data.index:
98
- # Display details of the selected row in the main area
99
- st.write("Selected Row Details:")
100
- selected_row = filtered_data.loc[st.session_state['last_clicked_row']]
101
- first_three_columns = selected_row.iloc[:3]
102
- for col in first_three_columns.index:
103
- st.write(f"{col}: {first_three_columns[col]}")
104
-
105
- # Display the HTML content for the selected row
106
- components.html(st.session_state['selected_row_html'], width=1280, height=1024)
107
-
108
-
109
 
110
  # Inject HTML5 and JavaScript for styling
111
  st.markdown("""
 
11
  data.append(json.loads(line))
12
  return pd.DataFrame(data)
13
 
14
+ # Your filter_by_keyword function
15
+ def filter_by_keyword(data, term):
16
+ # Your filtering logic here
17
+ return data[data['column_name'].str.contains(term)]
18
 
19
  # Function to generate HTML with textarea
20
+ def generate_html_with_textarea(row):
21
+ first_three_columns_text = ' '.join([f"{col}: {row[col]}" for col in row.index[:3]])
22
  return f'''
23
  <!DOCTYPE html>
24
  <html>
 
35
  <body>
36
  <h1>๐Ÿ”Š Read It Aloud</h1>
37
  <textarea id="textArea" rows="10" cols="80">
38
+ {first_three_columns_text}
39
  </textarea>
40
  <br>
41
  <button onclick="readAloud()">๐Ÿ”Š Read Aloud</button>
 
70
  st.session_state['last_clicked_row'] = None
71
 
72
 
73
+ # Streamlit app
74
  with st.expander("Search by Common Terms ๐Ÿ“š"):
75
  cols = st.columns(4)
76
  for term in top_20_terms:
 
79
  filtered_data = filter_by_keyword(data, term)
80
  st.write(f"Filter on '{term}' ๐Ÿ“Š")
81
 
82
+ # Display clickable rows in the dataframe
83
+ for idx, row in filtered_data.iterrows():
84
+ link = f"[Row {idx}]('javascript:void(0);')"
85
+ st.markdown(link, unsafe_allow_html=True)
86
+
87
+ # Capture the clicked row index
88
+ row_index = st.experimental_get_query_params().get("row_index")
89
+ if row_index:
90
+ selected_row = filtered_data.loc[int(row_index[0])]
91
+ html_content = generate_html_with_textarea(selected_row)
92
+ components.html(html_content, width=1280, height=1024)
93
+
94
+ # Always show the first row
95
+ if not filtered_data.empty:
96
+ first_row_html = generate_html_with_textarea(filtered_data.iloc[0])
97
+ components.html(first_row_html, width=1280, height=1024)
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  # Inject HTML5 and JavaScript for styling
100
  st.markdown("""