JSenkCC commited on
Commit
7fb5d1a
·
verified ·
1 Parent(s): 7c3b325

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -82
app.py CHANGED
@@ -470,55 +470,29 @@ def generate_documentation_page():
470
 
471
  # Button to generate PDF
472
  if st.button("Generate PDF"):
473
- pdf = FPDF()
474
- pdf.set_auto_page_break(auto=True, margin=15)
475
- pdf.add_page()
476
- pdf.set_font("Courier", size=12)
477
-
478
- # Write documentation to PDF with headers
479
- for line in documentation.split("\n"):
480
- if line.startswith("Project Summary:") or line.startswith("Functionality Summary:") or \
481
- line.startswith("Functionality Flow:") or line.startswith("Function Documentation:"):
482
- pdf.set_font("Courier", style="B", size=14)
483
- elif line.startswith("- '"):
484
- pdf.set_font("Courier", style="B", size=12)
485
- else:
486
- pdf.set_font("Courier", size=12)
487
- pdf.multi_cell(0, 10, line)
488
-
489
- # Save and allow download of the PDF
490
- pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
491
- pdf.output(pdf_file.name)
492
- st.download_button(
493
- label="Download PDF",
494
- data=open(pdf_file.name, "rb").read(),
495
- file_name=f"{st.session_state.current_project} Documentation.pdf",
496
- mime="application/pdf",
497
- )
498
- os.unlink(pdf_file.name)
499
 
500
  # Button to generate Markdown file
501
  if st.button("Generate Markdown File"):
502
- markdown_file_path = os.path.join(user_folder, f"{st.session_state.current_project} Documentation.md")
503
- with open(markdown_file_path, "w") as md_file:
504
- md_file.write(f"# Documentation for {st.session_state.current_project}\n\n")
505
- md_file.write(documentation)
506
-
507
- # Save and allow download of the Markdown file
508
- with open(markdown_file_path, "rb") as md_file:
509
- st.download_button(
510
- label="Download Markdown File",
511
- data=md_file.read(),
512
- file_name=f"{st.session_state.current_project} Documentation.md",
513
- mime="text/markdown",
514
- )
515
 
516
  # Helper function to generate PDF
517
- def generate_pdf(documentation):
518
  pdf = FPDF()
519
  pdf.set_auto_page_break(auto=True, margin=15)
520
  pdf.add_page()
521
- pdf.set_font("Courier", size=12) # Use Courier for an IDE-like font
522
 
523
  # Add headers and content
524
  for line in documentation.splitlines():
@@ -526,75 +500,88 @@ def generate_pdf(documentation):
526
  pdf.set_font("Courier", style="B", size=12) # Bold for specific lines
527
  pdf.multi_cell(0, 10, line)
528
  elif line.startswith("Project Summary:") or line.startswith("Functionality Summary:") or \
529
- line.startswith("Functionality Flow:") or line.startswith("Function Documentation:"):
530
  pdf.set_font("Courier", style="B", size=14) # Bold larger headers
531
  pdf.multi_cell(0, 10, line)
532
  else:
533
  pdf.set_font("Courier", size=12) # Regular for other lines
534
  pdf.multi_cell(0, 10, line)
535
 
536
- # Save and download the PDF
537
- project_name = st.session_state.current_project
538
- pdf_file_name = f"{project_name} Documentation.pdf"
539
- pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
540
- pdf.output(pdf_file.name)
541
- st.download_button(
542
- label="Download PDF",
543
- data=open(pdf_file.name, "rb").read(),
544
- file_name=pdf_file_name,
545
- mime="application/pdf",
546
- )
547
- os.unlink(pdf_file.name)
548
 
549
 
550
- # Helper function to generate Markdown file
551
- def generate_markdown_file(documentation):
552
- # Ensure the user folder is defined
553
- user_folder = os.path.join("user_projects", st.session_state.username)
554
- os.makedirs(user_folder, exist_ok=True)
555
 
556
- # Generate the Markdown file name
557
- project_name = st.session_state.current_project
558
- markdown_file_name = f"{project_name} Documentation.md"
 
559
 
560
- # Format documentation with markdown-specific rules
 
 
 
561
  formatted_lines = []
562
  for line in documentation.splitlines():
 
563
  if line.startswith("- '") and line.endswith("':"):
564
- formatted_lines.append(f"**{line}**") # Bold specific lines
 
565
  elif line.startswith("Project Summary:") or line.startswith("Functionality Summary:") or \
566
  line.startswith("Functionality Flow:") or line.startswith("Function Documentation:"):
567
- formatted_lines.append(f"# {line}") # Header for large sections
568
  else:
 
569
  formatted_lines.append(line)
570
 
571
- formatted_documentation = "\n".join(formatted_lines)
 
 
572
 
573
- # Save as a temporary Markdown file
574
- markdown_file = tempfile.NamedTemporaryFile(delete=False, suffix=".md")
575
- with open(markdown_file.name, "w") as f:
576
- f.write(formatted_documentation)
577
-
578
- # Provide download button for the Markdown file
579
- st.download_button(
580
- label="Download Markdown File",
581
- data=open(markdown_file.name, "rb").read(),
582
- file_name=markdown_file_name,
583
- mime="text/markdown",
584
- )
585
- os.unlink(markdown_file.name)
586
 
587
 
588
  #------------------------------------------------------------------------------------------------------------------------------------------------------------------------
589
-
590
-
591
  def view_documentation_page():
592
  st.subheader(f"View Documentation for {st.session_state.current_project}")
593
- st.write("This page will display the generated documentation for the selected project.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594
  if st.button("Back to Project"):
595
  st.session_state.page = "project_view"
596
  st.rerun()
597
 
 
 
598
  def project_view_page():
599
  # Sidebar with logout and return buttons
600
  st.sidebar.title(f"Project: {st.session_state.current_project}")
 
470
 
471
  # Button to generate PDF
472
  if st.button("Generate PDF"):
473
+ pdf_file_path = os.path.join(project_folder, f"{st.session_state.current_project} Documentation.pdf")
474
+ generate_pdf(documentation, pdf_file_path)
475
+
476
+ # Save the path in session state for later viewing
477
+ st.session_state.pdf_file_path = pdf_file_path
478
+ st.success("PDF file generated successfully! It will now appear in the 'View Documentation' page.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
 
480
  # Button to generate Markdown file
481
  if st.button("Generate Markdown File"):
482
+ markdown_file_path = os.path.join(project_folder, f"{st.session_state.current_project} Documentation.md")
483
+ generate_markdown_file(documentation, markdown_file_path)
484
+
485
+ # Save the path in session state for later viewing
486
+ st.session_state.markdown_file_path = markdown_file_path
487
+ st.success("Markdown file generated successfully! It will now appear in the 'View Documentation' page.")
488
+
 
 
 
 
 
 
489
 
490
  # Helper function to generate PDF
491
+ def generate_pdf(documentation, output_path):
492
  pdf = FPDF()
493
  pdf.set_auto_page_break(auto=True, margin=15)
494
  pdf.add_page()
495
+ pdf.set_font("Courier", size=12)
496
 
497
  # Add headers and content
498
  for line in documentation.splitlines():
 
500
  pdf.set_font("Courier", style="B", size=12) # Bold for specific lines
501
  pdf.multi_cell(0, 10, line)
502
  elif line.startswith("Project Summary:") or line.startswith("Functionality Summary:") or \
503
+ line.startswith("Functionality Flow:") or line.startswith("Function Documentation:"):
504
  pdf.set_font("Courier", style="B", size=14) # Bold larger headers
505
  pdf.multi_cell(0, 10, line)
506
  else:
507
  pdf.set_font("Courier", size=12) # Regular for other lines
508
  pdf.multi_cell(0, 10, line)
509
 
510
+ # Save the PDF
511
+ pdf.output(output_path)
 
 
 
 
 
 
 
 
 
 
512
 
513
 
 
 
 
 
 
514
 
515
+ # Helper function to generate Markdown file
516
+ def generate_markdown_file(documentation, output_path):
517
+ """
518
+ Generates a markdown file from the documentation with structured formatting.
519
 
520
+ Args:
521
+ documentation (str): The documentation content to format and save.
522
+ output_path (str): Path to save the generated markdown file.
523
+ """
524
  formatted_lines = []
525
  for line in documentation.splitlines():
526
+ # Add bold formatting to specific lines (e.g., function names)
527
  if line.startswith("- '") and line.endswith("':"):
528
+ formatted_lines.append(f"**{line.strip()}**")
529
+ # Add section headers with markdown syntax
530
  elif line.startswith("Project Summary:") or line.startswith("Functionality Summary:") or \
531
  line.startswith("Functionality Flow:") or line.startswith("Function Documentation:"):
532
+ formatted_lines.append(f"# {line.strip()}")
533
  else:
534
+ # Regular text as-is
535
  formatted_lines.append(line)
536
 
537
+ # Write the formatted lines to a markdown file
538
+ with open(output_path, "w") as f:
539
+ f.write("\n".join(formatted_lines))
540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
 
542
 
543
  #------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
 
544
  def view_documentation_page():
545
  st.subheader(f"View Documentation for {st.session_state.current_project}")
546
+ st.write("This page displays the generated documentation for the selected project.")
547
+
548
+ # Check if files are available in session state
549
+ pdf_path = st.session_state.get("pdf_file_path")
550
+ markdown_path = st.session_state.get("markdown_file_path")
551
+
552
+ # Display PDF file if it exists
553
+ if pdf_path and os.path.exists(pdf_path):
554
+ st.write("### PDF Documentation")
555
+ with open(pdf_path, "rb") as pdf_file:
556
+ pdf_data = pdf_file.read()
557
+ st.download_button(
558
+ label="Download PDF",
559
+ data=pdf_data,
560
+ file_name=os.path.basename(pdf_path),
561
+ mime="application/pdf",
562
+ )
563
+ st.write("View PDF Documentation below:")
564
+ st.pdf_viewer(pdf_path)
565
+
566
+ # Display Markdown file if it exists
567
+ if markdown_path and os.path.exists(markdown_path):
568
+ st.write("### Markdown Documentation")
569
+ with open(markdown_path, "r") as md_file:
570
+ markdown_content = md_file.read()
571
+ st.download_button(
572
+ label="Download Markdown",
573
+ data=markdown_content,
574
+ file_name=os.path.basename(markdown_path),
575
+ mime="text/markdown",
576
+ )
577
+ st.markdown(f"```\n{markdown_content}\n```", unsafe_allow_html=True)
578
+
579
  if st.button("Back to Project"):
580
  st.session_state.page = "project_view"
581
  st.rerun()
582
 
583
+
584
+
585
  def project_view_page():
586
  # Sidebar with logout and return buttons
587
  st.sidebar.title(f"Project: {st.session_state.current_project}")