Niharmahesh commited on
Commit
7bc447d
·
verified ·
1 Parent(s): a928259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -77
app.py CHANGED
@@ -287,95 +287,61 @@ def display_articles():
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 (safer approach)
364
- js_code = """
365
- <script>
366
- function openArticle() {
367
- var newWindow = window.open();
368
- var content = """ + repr(html_content) + """;
369
- newWindow.document.write(content);
370
- newWindow.document.close();
371
- }
372
- openArticle();
373
- </script>
374
- """
375
- st.markdown(js_code, unsafe_allow_html=True)
376
-
377
- except Exception as e:
378
- st.error(f"Error opening article: {str(e)}")
379
 
380
  def display_apps():
381
  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 view:")
291
+ st.markdown("") # Add some space
292
 
293
+ # Display each article as a clean clickable card
294
+ for i, html_file in enumerate(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
+ # Get file size
300
+ try:
301
+ file_size = os.path.getsize(html_file)
302
+ size_kb = round(file_size / 1024, 1)
303
+ size_text = f"{size_kb} KB"
304
+ except:
305
+ size_text = "- KB"
 
 
 
 
 
 
 
 
 
306
 
307
+ # Create a clean card-like button
308
+ article_clicked = st.button(
309
+ f"📄 {display_name} ({size_text})",
310
+ key=f"article_{i}",
311
+ use_container_width=True
312
+ )
 
 
313
 
314
+ if article_clicked:
 
315
  try:
316
  with open(html_file, 'r', encoding='utf-8') as file:
317
  html_content = file.read()
318
 
319
+ # Display article content inline
320
+ st.markdown("---")
321
+ st.markdown(f"### {display_name}")
322
+
323
+ # Add download option
324
+ col1, col2 = st.columns([1, 4])
325
+ with col1:
326
+ st.download_button(
327
+ label="⬇️ Download",
328
+ data=html_content,
329
+ file_name=os.path.basename(html_file),
330
+ mime="text/html",
331
+ key=f"download_clicked_{i}"
332
+ )
333
+ with col2:
334
+ if st.button("❌ Close Article", key=f"close_{i}"):
335
+ st.rerun()
336
+
337
+ # Display the HTML content
338
+ st.components.v1.html(html_content, height=600, scrolling=True)
339
+
340
  except Exception as e:
341
+ st.error(f"Error loading article: {str(e)}")
342
+
343
+ # Add small spacing between articles
344
+ st.markdown("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
  def display_apps():
347
  st.markdown('## Apps')