Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -287,41 +287,90 @@ def display_articles():
|
|
287 |
# Sort files by name for consistent ordering
|
288 |
html_files.sort()
|
289 |
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
|
296 |
-
|
297 |
-
options=range(len(html_files)),
|
298 |
-
format_func=lambda x: file_options[x]
|
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 |
def display_apps():
|
327 |
st.markdown('## Apps')
|
|
|
287 |
# Sort files by name for consistent ordering
|
288 |
html_files.sort()
|
289 |
|
290 |
+
st.markdown("Click on any article below to open it in a new tab:")
|
291 |
+
st.markdown("---")
|
292 |
+
|
293 |
+
# Display each article as a clickable link
|
294 |
+
for html_file in html_files:
|
295 |
+
# Extract filename without path and extension, format it nicely
|
296 |
+
file_name = os.path.splitext(os.path.basename(html_file))[0]
|
297 |
+
display_name = file_name.replace('_', ' ').replace('-', ' ').title()
|
298 |
|
299 |
+
# Create columns for better layout
|
300 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
|
|
|
|
|
|
301 |
|
302 |
+
with col1:
|
303 |
+
# Create clickable link that opens in new tab
|
304 |
+
file_url = f"file://{os.path.abspath(html_file)}"
|
305 |
+
st.markdown(f"📄 **[{display_name}]({file_url})**", unsafe_allow_html=True)
|
306 |
+
|
307 |
+
# Add a JavaScript link that opens in new tab (alternative method)
|
308 |
+
st.markdown(f"""
|
309 |
+
<a href="file://{os.path.abspath(html_file)}" target="_blank" style="text-decoration: none;">
|
310 |
+
<div style="padding: 10px; border: 1px solid #ddd; border-radius: 5px; margin: 5px 0; background-color: #f9f9f9; cursor: pointer;">
|
311 |
+
📄 {display_name}
|
312 |
+
</div>
|
313 |
+
</a>
|
314 |
+
""", unsafe_allow_html=True)
|
315 |
|
316 |
+
with col2:
|
317 |
+
# Add file size info
|
318 |
+
try:
|
319 |
+
file_size = os.path.getsize(html_file)
|
320 |
+
size_kb = round(file_size / 1024, 1)
|
321 |
+
st.text(f"{size_kb} KB")
|
322 |
+
except:
|
323 |
+
st.text("- KB")
|
324 |
|
325 |
+
with col3:
|
326 |
+
# Add download button for each file
|
327 |
+
try:
|
328 |
+
with open(html_file, 'r', encoding='utf-8') as file:
|
329 |
+
html_content = file.read()
|
330 |
+
|
331 |
+
st.download_button(
|
332 |
+
label="⬇️",
|
333 |
+
data=html_content,
|
334 |
+
file_name=os.path.basename(html_file),
|
335 |
+
mime="text/html",
|
336 |
+
key=f"download_{file_name}"
|
337 |
+
)
|
338 |
+
except Exception as e:
|
339 |
+
st.text("Error")
|
340 |
+
|
341 |
+
# Alternative: Serve files through Streamlit (if file:// doesn't work)
|
342 |
+
st.markdown("---")
|
343 |
+
st.info("💡 **Alternative Method**: If the links above don't work, select an article below to view it inline:")
|
344 |
+
|
345 |
+
# Fallback option with selectbox
|
346 |
+
file_options = [os.path.splitext(os.path.basename(f))[0].replace('_', ' ').title() for f in html_files]
|
347 |
+
|
348 |
+
selected_idx = st.selectbox(
|
349 |
+
"Choose an article to view inline:",
|
350 |
+
options=range(len(html_files)),
|
351 |
+
format_func=lambda x: file_options[x],
|
352 |
+
key="inline_article_selector"
|
353 |
+
)
|
354 |
+
|
355 |
+
if st.button("Open Selected Article in New Tab"):
|
356 |
+
selected_file = html_files[selected_idx]
|
357 |
|
358 |
+
# Create a temporary serving method using Streamlit's built-in server
|
359 |
+
try:
|
360 |
+
with open(selected_file, 'r', encoding='utf-8') as file:
|
361 |
+
html_content = file.read()
|
362 |
+
|
363 |
+
# Use JavaScript to open content in new window
|
364 |
+
st.markdown(f"""
|
365 |
+
<script>
|
366 |
+
var newWindow = window.open();
|
367 |
+
newWindow.document.write(`{html_content.replace('`', '\\`')}`);
|
368 |
+
newWindow.document.close();
|
369 |
+
</script>
|
370 |
+
""", unsafe_allow_html=True)
|
371 |
+
|
372 |
+
except Exception as e:
|
373 |
+
st.error(f"Error opening article: {str(e)}")
|
374 |
|
375 |
def display_apps():
|
376 |
st.markdown('## Apps')
|