1+ """
2+ StreamlitReportView class for generating Streamlit reports based on a configuration file.
3+ """
4+
15import os
26import subprocess
37import sys
1418
1519
1620def write_python_file (fpath : str , imports : list [str ], contents : list [str ]) -> None :
21+ """Write a Python file with the given imports and contents."""
1722 with open (fpath , "w" , encoding = "utf8" ) as f :
1823 # Write imports at the top of the file
1924 f .write ("\n " .join (imports ) + "\n \n " )
@@ -75,12 +80,14 @@ def __init__(
7580
7681 def generate_report (self , output_dir : str = SECTIONS_DIR ) -> None :
7782 """
78- Generates the Streamlit report and creates Python files for each section and its subsections and plots.
83+ Generates the Streamlit report and creates Python files for each section
84+ and its subsections and plots.
7985
8086 Parameters
8187 ----------
8288 output_dir : str, optional
83- The folder where the generated report files will be saved (default is SECTIONS_DIR).
89+ The folder where the generated report files will be saved
90+ (default is SECTIONS_DIR).
8491 """
8592 self .report .logger .debug (
8693 f"Generating '{ self .report_type } ' report in directory: '{ output_dir } '"
@@ -167,14 +174,17 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
167174 self .report .logger .debug (
168175 f"Section directory already existed: { section_dir_path } "
169176 )
170- # add an overview page to section of components exist
177+ # add an overview page to section for it's section components
178+ # they will be written when the components are parsed
179+ # using `_generate_sections`
171180 if section .components :
172181 subsection_file_path = (
173182 Path (section_name_var )
174183 / f"0_overview_{ make_valid_identifier (section .title ).lower ()} .py"
175184 ).as_posix () # Make sure it's Posix Paths
176185 section .file_path = subsection_file_path
177- # Create a Page object for each subsection and add it to the home page content
186+ # Create a Page object for each subsection and
187+ # add it to the home page content
178188 report_manag_content .append (
179189 f"{ section_name_var } _overview = st.Page('{ subsection_file_path } ', title='Overview { section .title } ')"
180190 )
@@ -194,7 +204,8 @@ def generate_report(self, output_dir: str = SECTIONS_DIR) -> None:
194204 Path (section_name_var ) / f"{ subsection_name_var } .py"
195205 ).as_posix () # Make sure it's Posix Paths
196206 subsection .file_path = subsection_file_path
197- # Create a Page object for each subsection and add it to the home page content
207+ # Create a Page object for each subsection and
208+ # add it to the home page content
198209 report_manag_content .append (
199210 f"{ subsection_name_var } = st.Page('{ subsection_file_path } ', title='{ subsection .title } ')"
200211 )
@@ -411,7 +422,8 @@ def _generate_home_section(
411422
412423 # Add the home page to the report manager content
413424 report_manag_content .append (
414- "homepage = st.Page('Home/Homepage.py', title='Homepage')" # ! here Posix Path is hardcoded
425+ # ! here Posix Path is hardcoded
426+ "homepage = st.Page('Home/Homepage.py', title='Homepage')"
415427 )
416428 report_manag_content .append ("sections_pages['Home'] = [homepage]\n " )
417429 self .report .logger .info ("Home page added to the report manager content." )
@@ -421,7 +433,8 @@ def _generate_home_section(
421433
422434 def _generate_sections (self , output_dir : str ) -> None :
423435 """
424- Generates Python files for each section in the report, including subsections and its components (plots, dataframes, markdown).
436+ Generates Python files for each section in the report, including subsections
437+ and its components (plots, dataframes, markdown).
425438
426439 Parameters
427440 ----------
@@ -458,7 +471,9 @@ def _generate_sections(self, output_dir: str) -> None:
458471 continue
459472
460473 # Iterate through subsections and integrate them into the section file
461- # subsection should have the subsection_file_path as file_path?
474+ # ! subsection should have the subsection_file_path as file_path,
475+ # ! which is set when parsing the config in the main generate_sections
476+ # ! method
462477 for subsection in section .subsections :
463478 self .report .logger .debug (
464479 f"Processing subsection '{ subsection .id } ': '{ subsection .title } -"
@@ -603,7 +618,8 @@ def _generate_plot_content(self, plot) -> List[str]:
603618 # If network_data is a tuple, separate the network and html file path
604619 networkx_graph , html_plot_file = networkx_graph
605620 else :
606- # Otherwise, create and save a new pyvis network from the netowrkx graph
621+ # Otherwise,
622+ # create and save a new pyvis network from the netowrkx graph
607623 html_plot_file = (
608624 Path (self .static_dir ) / f"{ plot .title .replace (' ' , '_' )} .html"
609625 ).resolve ()
@@ -737,7 +753,8 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
737753 f"Unsupported file extension: { file_extension } . Supported extensions are: { ', ' .join (fmt .value for fmt in r .DataFrameFormat )} ."
738754 )
739755 # return [] # Skip execution if unsupported file extension
740- # Should it not return here? Can we even call the method with an unsupported file extension?
756+ # Should it not return here?
757+ # Can we even call the method with an unsupported file extension?
741758
742759 # Build the file path (URL or local file)
743760 if is_url (dataframe .file_path ):
@@ -1067,7 +1084,7 @@ def generate_query(messages):
10671084 json={{"model": "{ chatbot .model } ", "messages": messages, "stream": True}},
10681085 )
10691086 response.raise_for_status()
1070- return response
1087+ return response
10711088
10721089# Parse streaming response from Ollama
10731090def parse_api_response(response):
0 commit comments