Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
358 lines (315 loc) · 10.2 KB
/
Copy pathapp.py
File metadata and controls
358 lines (315 loc) · 10.2 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import streamlit as st
import pandas as pd
import json
import os
from datetime import datetime, timedelta
import requests
from typing import Dict, List, Any, Optional
# Add parent directory to path for imports
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from utils.visualization import create_world_map, create_industry_chart
from utils.data_manager import (
initialize_session_data,
get_session_events_data,
fetch_tariff_events,
process_events_data,
get_events_statistics,
)
# Set page configuration
st.set_page_config(
page_title="Tariff Tracker",
page_icon="🌐",
layout="wide",
initial_sidebar_state="expanded",
)
# Add custom CSS for styling
st.markdown(
"""
<style>
.main-header {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 0.5rem;
}
.sub-header {
font-size: 1.5rem;
font-weight: 500;
color: #4d4d4d;
margin-bottom: 1.5rem;
}
.metric-card {
background-color: #f0f2f6;
border-radius: 0.5rem;
padding: 1rem;
margin-bottom: 1rem;
}
.metric-value {
font-size: 2rem;
font-weight: 700;
color: #0068c9;
}
.metric-label {
font-size: 1rem;
color: #4d4d4d;
}
.logo-text {
font-size: 1.5rem;
font-weight: 700;
margin-bottom: 2rem;
text-align: left;
font-family: monospace;
padding-top: 1rem;
padding-bottom: 1rem;
}
.about-section {
padding-top: 1rem;
}
.action-card {
background-color: #f0f2f6;
border-radius: 0.5rem;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.action-title {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 1rem;
}
.quick-link {
text-decoration: none;
color: #0068c9;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
background-color: #e6f2ff;
margin-right: 0.5rem;
margin-bottom: 0.5rem;
display: inline-block;
}
section[data-testid="stSidebar"] > div:first-child {
padding-top: 0;
}
</style>
""",
unsafe_allow_html=True,
)
# Add logo to sidebar
with st.sidebar:
# Logo as text
st.markdown(
'<div class="logo-text"></newscatcher></div>', unsafe_allow_html=True
)
# Add data refresh options
st.subheader("Data Options")
# Set debug mode as a session state variable but don't show in UI
if "debug_mode" not in st.session_state:
st.session_state.debug_mode = False
if st.button("Refresh Data"):
initialize_session_data(force_refresh=True)
st.success("Data refreshed successfully!")
# Show last update time if available
if "last_update_time" in st.session_state:
st.info(
f"Last updated: {st.session_state.last_update_time.strftime('%Y-%m-%d %H:%M')}"
)
# About section at the bottom of sidebar
st.markdown('<div class="about-section"></div>', unsafe_allow_html=True)
st.markdown("### About")
st.markdown(
"""
This app visualizes global tariff events extracted from news data.
Built with Streamlit and powered by the NewsCatcher Events API.
© 2025 NewsCatcher Inc.
"""
)
# Main content
st.markdown(
'<div class="main-header">Tariff Tracker</div>',
unsafe_allow_html=True,
)
st.markdown(
'<div class="sub-header">Instant insights into global tariff events and trade policies</div>',
unsafe_allow_html=True,
)
# Quick action card
with st.container():
st.markdown(
"""
<div class="action-card">
<div class="action-title">Get Started</div>
<p>View the most recent tariff events or use the API Query Builder to customize your search.</p>
</div>
""",
unsafe_allow_html=True,
)
# Quick action buttons in two columns
col1, col2 = st.columns(2)
with col1:
hours_to_look_back = st.slider(
"Hours to look back:",
min_value=6,
max_value=48,
value=24,
step=6,
help="Select how many hours of data to fetch",
)
with col2:
# Add a fetch button
fetch_data = st.button("Fetch Recent Events", type="primary")
# If the fetch button is pressed, fetch new data
if fetch_data:
with st.spinner("Fetching recent tariff events..."):
# Fetch the data with the specified time range
api_result = fetch_tariff_events(hours_to_look_back=hours_to_look_back)
# Process the data and update session state
processed_events, events_df = process_events_data(api_result)
stats = get_events_statistics(processed_events)
# Update session state
st.session_state.api_result = api_result
st.session_state.processed_events = processed_events
st.session_state.events_df = events_df
st.session_state.stats = stats
st.session_state.events_initialized = True
st.session_state.last_update_time = datetime.now()
st.success(
f"Successfully fetched {len(processed_events)} events from the past {hours_to_look_back} hours!"
)
else:
# Initialize data if needed
if "events_initialized" not in st.session_state:
with st.spinner("Loading initial data..."):
initialize_session_data()
# Get the current data from session state
api_result, processed_events, events_df, stats = get_session_events_data()
# Display metrics if we have data
if processed_events:
# Display metrics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown(
f"""
<div class="metric-card">
<div class="metric-value">{stats['total_events']}</div>
<div class="metric-label">Total Events</div>
</div>
""",
unsafe_allow_html=True,
)
with col2:
st.markdown(
f"""
<div class="metric-card">
<div class="metric-value">{len(stats['imposing_countries'])}</div>
<div class="metric-label">Imposing Countries</div>
</div>
""",
unsafe_allow_html=True,
)
with col3:
st.markdown(
f"""
<div class="metric-card">
<div class="metric-value">{len(stats['targeted_countries'])}</div>
<div class="metric-label">Targeted Countries</div>
</div>
""",
unsafe_allow_html=True,
)
with col4:
st.markdown(
f"""
<div class="metric-card">
<div class="metric-value">{len(stats['affected_products'])}</div>
<div class="metric-label">Affected Products</div>
</div>
""",
unsafe_allow_html=True,
)
# Simple visualization
st.subheader("Global Tariff Heatmap")
map_type = st.radio(
"Select map type:",
["Imposing Countries", "Targeted Countries"],
horizontal=True,
)
# Create the map visualization (use debug mode from session state)
map_fig = create_world_map(
events_df,
"imposing" if map_type == "Imposing Countries" else "targeted",
debug=st.session_state.debug_mode,
)
if map_fig:
st.plotly_chart(map_fig, use_container_width=True)
else:
st.info("Not enough data to create the map visualization.")
# Display recent events table
st.subheader("Recent Tariff Events")
if not events_df.empty:
# Show only essential columns for the table view
display_cols = [
"announcement_date",
"imposing_country",
"targeted_countries",
"measure_type",
"main_tariff_rate",
"relevance_score",
]
# Filter for display columns that actually exist in the dataframe
existing_cols = [col for col in display_cols if col in events_df.columns]
# Display table
st.dataframe(events_df[existing_cols], use_container_width=True)
# Option to download results
csv = events_df.to_csv(index=False).encode("utf-8")
st.download_button(
label="Download as CSV",
data=csv,
file_name=f"tariff_events_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
mime="text/csv",
)
# Option to view raw JSON
with st.expander("View raw API response"):
st.code(json.dumps(api_result, indent=2), language="json")
else:
st.warning("No events found in the data")
else:
st.error("Failed to load data. Please check the API connection or sample data.")
# Navigation cards section
st.markdown("## Explore Insights")
# Create three columns for navigation cards
col1, col2, col3 = st.columns(3)
with col1:
st.markdown(
"""
<div class="action-card">
<div class="action-title">Tariff Event Explorer</div>
<p>Browse and filter individual tariff events in detail. View source articles and analyze event information.</p>
<a href="/Event_Explorer" target="_self" class="quick-link">Explore Events →</a>
</div>
""",
unsafe_allow_html=True,
)
with col2:
st.markdown(
"""
<div class="action-card">
<div class="action-title">Industry Analysis</div>
<p>Explore how tariffs impact different industries and product categories. Identify trends across sectors.</p>
<a href="/Industry_Analysis" target="_self" class="quick-link">Analyze Industries →</a>
</div>
""",
unsafe_allow_html=True,
)
with col3:
st.markdown(
"""
<div class="action-card">
<div class="action-title">API Query Builder</div>
<p>Create custom queries to the Events API. Filter by date, countries, industries, and more.</p>
<a href="/API_Query_Builder" target="_self" class="quick-link">Build Queries →</a>
</div>
""",
unsafe_allow_html=True,
)
# Footer
st.markdown("---")
st.markdown("Built with ❤️ using Streamlit • Data provided by NewsCatcher Events API")