-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.py
More file actions
84 lines (65 loc) · 2.89 KB
/
home.py
File metadata and controls
84 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pandas as pd
import streamlit as st
import plotly.express as px
# Sidebar setup
st.sidebar.page_link(page="home.py", label="Keyword Search", icon="🔍")
st.sidebar.page_link(page="pages/about-data.py", label="About Data", icon="ℹ️")
# Main title of the app
st.title("Keyword Search in German Notes")
# Google Drive File ID (replace with your actual file ID)
file_id = "1edT0_Agv-HqZjMDQQykM7wNDOIA9h32N"
# Construct the direct download URL
gdrive_url = f"https://drive.google.com/uc?id={file_id}"
#df_prep_notes_de = pd.read_csv("data/df_X_German_preprocessed.csv", low_memory=False)
# Read CSV from Google Drive
@st.cache_data(ttl=3600)
def load_data():
return pd.read_csv(gdrive_url, low_memory=False)
df_prep_notes_de = load_data()
# df_prep_notes_de = pd.read_csv(gdrive_url, low_memory=False)
# st.write("Data loaded successfully:", df_prep_notes_de.shape)
# Check problematic columns and convert to strings
cols_to_fix = ['col_5_name', 'col_6_name', 'col_7_name'] # Replace with actual column names
for col in cols_to_fix:
if col in df_prep_notes_de.columns:
df_prep_notes_de[col] = df_prep_notes_de[col].astype(str)
# Sidebar input for keyword search
keyword_searched = st.text_input(label='Type your keyword', value='twitter')
# Placeholder for plot
plot = st.empty()
# If a keyword is entered, filter and display data
if keyword_searched.strip():
# Fix SettingWithCopyWarning: Use .copy()
filtered_df = df_prep_notes_de[
df_prep_notes_de['cleaned_summary'].str.contains(keyword_searched, case=False, na=False)
].copy()
# Convert 'date' safely, handle errors
filtered_df['date'] = pd.to_datetime(filtered_df['date'], errors='coerce')
# Drop NaT (invalid dates)
filtered_df = filtered_df.dropna(subset=['date'])
# Group by date and count occurrences
date_counts = filtered_df.groupby('date').size().reset_index(name="Number of Notes")
# Plotly line chart with proper labels
fig = px.line(
date_counts,
x='date',
y='Number of Notes',
title=f"Notes per Date for keyword: '{keyword_searched}'", # ✅ Fixed unterminated string issue
markers=True,
height=400
)
# Update axes and layout
fig.update_xaxes(title_text='Date', showgrid=True, gridwidth=1, gridcolor='lightgray')
fig.update_yaxes(title_text='Number of Notes', showgrid=True, gridwidth=1, gridcolor='lightgray')
fig.update_layout(
showlegend=False,
plot_bgcolor='white',
title_x=0.5,
title_font=dict(size=16, color='black'),
xaxis_title_font=dict(size=12, color='black'),
yaxis_title_font=dict(size=12, color='black')
)
# Display interactive Plotly chart
plot.plotly_chart(fig)
else:
st.write("Please enter a keyword in the textbar to search for it in the German notes. You have access to the preprocessed dataset at the moment where URLs are excluded.")