Niharmahesh commited on
Commit
d67ad02
·
verified ·
1 Parent(s): 3b8e24a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -30
app.py CHANGED
@@ -287,41 +287,90 @@ def display_articles():
287
  # Sort files by name for consistent ordering
288
  html_files.sort()
289
 
290
- # Create a selectbox to choose which article to display
291
- if len(html_files) > 1:
292
- # Extract just the filename without path and extension for display
293
- file_options = [os.path.splitext(os.path.basename(f))[0].replace('_', ' ').title() for f in html_files]
 
 
 
 
294
 
295
- selected_article = st.selectbox(
296
- "Choose an article to read:",
297
- options=range(len(html_files)),
298
- format_func=lambda x: file_options[x]
299
- )
300
 
301
- selected_file = html_files[selected_article]
302
- else:
303
- selected_file = html_files[0]
304
-
305
- # Display the selected article
306
- try:
307
- with open(selected_file, 'r', encoding='utf-8') as file:
308
- html_content = file.read()
 
 
 
 
 
309
 
310
- # Display the HTML content
311
- st.markdown("---")
312
- st.components.v1.html(html_content, height=600, scrolling=True)
 
 
 
 
 
313
 
314
- # Add download button
315
- st.download_button(
316
- label="Download Article",
317
- data=html_content,
318
- file_name=os.path.basename(selected_file),
319
- mime="text/html"
320
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
 
322
- except Exception as e:
323
- st.error(f"Error loading article: {str(e)}")
324
- st.info("Make sure your HTML files are properly formatted and encoded in UTF-8.")
 
 
 
 
 
 
 
 
 
 
 
 
 
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')