JSenkCC commited on
Commit
7ac17d1
·
verified ·
1 Parent(s): ff97c45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -94
app.py CHANGED
@@ -446,13 +446,6 @@ def generate_documentation_page():
446
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
447
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
448
 
449
- # Clear previously generated documentation files when starting a new analysis
450
- if st.session_state.get("new_analysis_started", False):
451
- st.session_state.pop("pdf_generated", None)
452
- st.session_state.pop("markdown_generated", None)
453
- st.session_state.pop("generated_documentation", None)
454
- st.session_state.new_analysis_started = False
455
-
456
  # Prompt user for functionality description
457
  functionality = st.text_area(
458
  "Describe the functionality",
@@ -462,36 +455,35 @@ def generate_documentation_page():
462
  # Button to start analyzing functionality
463
  if st.button("Analyze"):
464
  if functionality.strip():
465
- st.session_state.new_analysis_started = True
466
- st.write("Analyzing project files... Please wait.")
467
 
468
- # Get the path of the current project
469
- user_folder = os.path.join("user_projects", st.session_state.username)
470
- project_folder = os.path.join(user_folder, st.session_state.current_project)
471
 
472
- if os.path.exists(project_folder):
473
- try:
474
- # Gather all file paths in the project directory
475
- file_paths = read_project_files(project_folder)
476
 
477
- # Read file contents
478
- file_contents = read_files(file_paths)
479
 
480
- # Generate documentation using Gemini
481
- documentation = generate_detailed_documentation(file_contents, functionality)
482
 
483
- # Save the documentation in session state for exporting and viewing
484
- st.session_state.generated_documentation = documentation
485
 
486
- # Display the final documentation
487
- st.success("Documentation generated successfully!")
488
- st.text_area("Generated Documentation", documentation, height=600)
489
- except Exception as e:
490
- st.error(f"An error occurred: {e}")
 
 
491
  else:
492
- st.error("Project folder not found. Ensure the GitHub repository was cloned successfully.")
493
- else:
494
- st.error("Please enter the functionality to analyze.")
495
 
496
  # Add export/download buttons if documentation is available
497
  if "generated_documentation" in st.session_state and st.session_state.generated_documentation:
@@ -504,53 +496,50 @@ def generate_documentation_page():
504
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
505
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
506
 
507
- col1, col2 = st.columns(2)
 
 
508
 
509
- # PDF generation and download logic
510
- with col1:
511
- if not st.session_state.get("pdf_generated", False):
512
- if st.button("Generate PDF"):
513
- try:
514
- generate_pdf(documentation, pdf_path)
515
- st.session_state.pdf_generated = True
516
- st.success("PDF Available in the 'Saved Documentation' Page")
517
- st.rerun()
518
- except Exception as e:
519
- st.error(f"Failed to generate PDF: {e}")
520
- else:
521
  try:
522
- with open(pdf_path, "rb") as pdf_file:
523
- st.download_button(
524
- label="Download PDF",
525
- data=pdf_file.read(),
526
- file_name=f"{st.session_state.current_project}_Documentation.pdf",
527
- mime="application/pdf",
528
- )
529
- except FileNotFoundError:
530
- st.error("PDF file not found. Try generating it again.")
531
-
532
- # Markdown generation and download logic
533
- with col2:
534
- if not st.session_state.get("markdown_generated", False):
535
- if st.button("Generate Markdown File"):
536
- try:
537
- generate_markdown_file(documentation, markdown_path)
538
- st.session_state.markdown_generated = True
539
- st.success("Markdown Available in the 'Saved Documentation' Page")
540
- st.rerun()
541
- except Exception as e:
542
- st.error(f"Failed to generate Markdown file: {e}")
543
- else:
544
  try:
545
- with open(markdown_path, "rb") as markdown_file:
546
- st.download_button(
547
- label="Download Markdown File",
548
- data=markdown_file.read(),
549
- file_name=f"{st.session_state.current_project}_Documentation.md",
550
- mime="text/markdown",
551
- )
552
- except FileNotFoundError:
553
- st.error("Markdown file not found. Try generating it again.")
 
 
 
 
 
 
 
554
 
555
 
556
 
@@ -613,11 +602,12 @@ def generate_markdown_file(documentation, output_path):
613
 
614
  #------------------------------------------------------------------------------------------------------------------------------------------------------------------------
615
  def saved_documentation_page():
 
616
  st.sidebar.image("SimplifAI Logo Long.jpeg", use_container_width=True)
617
  st.sidebar.title(f"Project: {st.session_state.current_project}")
618
  if st.sidebar.button("Back to Project"):
619
  st.session_state.page = "project_view"
620
- st.experimental_rerun()
621
  if st.sidebar.button("Log Out"):
622
  logout_user()
623
  st.rerun()
@@ -630,25 +620,31 @@ def saved_documentation_page():
630
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
631
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
632
 
633
- # Check if files exist and display download options
634
- if os.path.exists(pdf_path):
635
- with open(pdf_path, "rb") as pdf_file:
636
- st.download_button(
637
- label="Download PDF Documentation",
638
- data=pdf_file.read(),
639
- file_name=f"{st.session_state.current_project}_Documentation.pdf",
640
- mime="application/pdf",
641
- )
642
- if os.path.exists(markdown_path):
643
- with open(markdown_path, "rb") as markdown_file:
644
- st.download_button(
645
- label="Download Markdown Documentation",
646
- data=markdown_file.read(),
647
- file_name=f"{st.session_state.current_project}_Documentation.md",
648
- mime="text/markdown",
649
- )
650
- if not os.path.exists(pdf_path) and not os.path.exists(markdown_path):
651
- st.info("No documentation has been generated yet.")
 
 
 
 
 
 
652
 
653
 
654
  def project_view_page():
 
446
  st.subheader(f"Generate Documentation for {st.session_state.current_project}")
447
  st.write("Enter the functionality or parts of the project for which you'd like to generate documentation.")
448
 
 
 
 
 
 
 
 
449
  # Prompt user for functionality description
450
  functionality = st.text_area(
451
  "Describe the functionality",
 
455
  # Button to start analyzing functionality
456
  if st.button("Analyze"):
457
  if functionality.strip():
458
+ with st.spinner("Analyzing project files... Please wait."):
 
459
 
460
+ # Get the path of the current project
461
+ user_folder = os.path.join("user_projects", st.session_state.username)
462
+ project_folder = os.path.join(user_folder, st.session_state.current_project)
463
 
464
+ if os.path.exists(project_folder):
465
+ try:
466
+ # Gather all file paths in the project directory
467
+ file_paths = read_project_files(project_folder)
468
 
469
+ # Read file contents
470
+ file_contents = read_files(file_paths)
471
 
472
+ # Generate documentation using Gemini
473
+ documentation = generate_detailed_documentation(file_contents, functionality)
474
 
475
+ # Save the documentation in session state for exporting and viewing
476
+ st.session_state.generated_documentation = documentation
477
 
478
+ # Display the final documentation
479
+ st.success("Documentation generated successfully!")
480
+ st.text_area("Generated Documentation", documentation, height=600)
481
+ except Exception as e:
482
+ st.error(f"An error occurred: {e}")
483
+ else:
484
+ st.error("Project folder not found. Ensure the GitHub repository was cloned successfully.")
485
  else:
486
+ st.error("Please enter the functionality to analyze.")
 
 
487
 
488
  # Add export/download buttons if documentation is available
489
  if "generated_documentation" in st.session_state and st.session_state.generated_documentation:
 
496
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
497
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
498
 
499
+ # Show appropriate buttons based on file generation status
500
+ pdf_generated = os.path.exists(pdf_path)
501
+ markdown_generated = os.path.exists(markdown_path)
502
 
503
+ # Generate and replace buttons dynamically
504
+ if not pdf_generated:
505
+ if st.button("Generate PDF"):
 
 
 
 
 
 
 
 
 
506
  try:
507
+ generate_pdf(documentation, pdf_path)
508
+ st.session_state.pdf_generated = True # Set a flag for PDF generation
509
+ st.rerun() # Trigger a rerun to show the download button
510
+ except Exception as e:
511
+ st.error(f"Failed to generate PDF: {e}")
512
+ else:
513
+ try:
514
+ with open(pdf_path, "rb") as pdf_file:
515
+ st.download_button(
516
+ label="Download PDF",
517
+ data=pdf_file.read(),
518
+ file_name=f"{st.session_state.current_project}_Documentation.pdf",
519
+ mime="application/pdf",
520
+ )
521
+ except FileNotFoundError:
522
+ st.write("Click 'Generate PDF' to be able to download and store the documentation as a PDF")
523
+
524
+ if not markdown_generated:
525
+ if st.button("Generate Markdown File"):
 
 
 
526
  try:
527
+ generate_markdown_file(documentation, markdown_path)
528
+ st.session_state.markdown_generated = True # Set a flag for Markdown generation
529
+ st.rerun() # Trigger a rerun to show the download button
530
+ except Exception as e:
531
+ st.error(f"Failed to generate Markdown file: {e}")
532
+ else:
533
+ try:
534
+ with open(markdown_path, "rb") as markdown_file:
535
+ st.download_button(
536
+ label="Download Markdown File",
537
+ data=markdown_file.read(),
538
+ file_name=f"{st.session_state.current_project}_Documentation.md",
539
+ mime="text/markdown",
540
+ )
541
+ except FileNotFoundError:
542
+ st.write("Click 'Generate Markdown File' to be able to download and store the documentation as a markdown file")
543
 
544
 
545
 
 
602
 
603
  #------------------------------------------------------------------------------------------------------------------------------------------------------------------------
604
  def saved_documentation_page():
605
+ # Sidebar with "Back to Project" and "Log Out" buttons
606
  st.sidebar.image("SimplifAI Logo Long.jpeg", use_container_width=True)
607
  st.sidebar.title(f"Project: {st.session_state.current_project}")
608
  if st.sidebar.button("Back to Project"):
609
  st.session_state.page = "project_view"
610
+ st.rerun()
611
  if st.sidebar.button("Log Out"):
612
  logout_user()
613
  st.rerun()
 
620
  pdf_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.pdf")
621
  markdown_path = os.path.join(user_folder, f"{st.session_state.current_project}_Documentation.md")
622
 
623
+ # Check if files exist
624
+ pdf_exists = os.path.exists(pdf_path)
625
+ markdown_exists = os.path.exists(markdown_path)
626
+
627
+ # Display available documentation
628
+ if not pdf_exists and not markdown_exists:
629
+ st.info("No documentation has been generated yet. Please go to the 'Generate Documentation' page.")
630
+ else:
631
+ if pdf_exists:
632
+ with open(pdf_path, "rb") as pdf_file:
633
+ st.download_button(
634
+ label="Download PDF Documentation",
635
+ data=pdf_file.read(),
636
+ file_name=f"{st.session_state.current_project}_Documentation.pdf",
637
+ mime="application/pdf",
638
+ )
639
+
640
+ if markdown_exists:
641
+ with open(markdown_path, "rb") as markdown_file:
642
+ st.download_button(
643
+ label="Download Markdown Documentation",
644
+ data=markdown_file.read(),
645
+ file_name=f"{st.session_state.current_project}_Documentation.md",
646
+ mime="text/markdown",
647
+ )
648
 
649
 
650
  def project_view_page():