@@ -265,54 +265,50 @@ def _generate_plot_content(self, plot, is_report_static, static_dir: str = STATI
265265 plot_content = []
266266 # Add title
267267 plot_content .append (f'### { plot .title } ' )
268+
269+ # Define plot path
270+ if is_report_static :
271+ static_plot_path = os .path .join (static_dir , f"{ plot .title .replace (' ' , '_' )} .png" )
272+ else :
273+ html_plot_file = os .path .join (static_dir , f"{ plot .title .replace (' ' , '_' )} .html" )
268274
269- if plot .plot_type == r .PlotType .INTERACTIVE :
270- try :
271- # Define plot path
275+ # Add content for the different plot types
276+ try :
277+ if plot .plot_type == r .PlotType .STATIC :
278+ plot_content .append (self ._generate_image_content (plot .file_path , width = 950 ))
279+ elif plot .plot_type == r .PlotType .PLOTLY :
280+ plot_content .append (self ._generate_plot_code (plot ))
272281 if is_report_static :
273- static_plot_path = os .path .join (static_dir , f"{ plot .title .replace (' ' , '_' )} .png" )
282+ plot_content .append (f"""fig_plotly.write_image("{ os .path .join (".." , static_plot_path )} ")\n ```\n """ )
283+ plot_content .append (self ._generate_image_content (static_plot_path ))
274284 else :
275- html_plot_file = os .path .join (static_dir , f"{ plot .title .replace (' ' , '_' )} .html" )
276-
277- if plot .int_visualization_tool == r .IntVisualizationTool .PLOTLY :
278- plot_content .append (self ._generate_plot_code (plot ))
279- if is_report_static :
280- plot_content .append (f"""fig_plotly.write_image("{ os .path .join (".." , static_plot_path )} ")\n ```\n """ )
281- plot_content .append (self ._generate_image_content (static_plot_path ))
282- else :
283- plot_content .append (f"""fig_plotly.show()\n ```\n """ )
284- elif plot .int_visualization_tool == r .IntVisualizationTool .ALTAIR :
285- plot_content .append (self ._generate_plot_code (plot ))
286- if is_report_static :
287- plot_content .append (f"""fig_altair.save("{ os .path .join (".." , static_plot_path )} ")\n ```\n """ )
288- plot_content .append (self ._generate_image_content (static_plot_path ))
289- else :
290- plot_content .append (f"""fig_altair\n ```\n """ )
291- elif plot .int_visualization_tool == r .IntVisualizationTool .PYVIS :
292- G = plot .read_network ()
293- num_nodes = G .number_of_nodes ()
294- num_edges = G .number_of_edges ()
295- plot_content .append (f'**Number of nodes:** { num_nodes } \n ' )
296- plot_content .append (f'**Number of edges:** { num_edges } \n ' )
297- if is_report_static :
298- plot .save_netwrok_image (G , static_plot_path , "png" )
299- plot_content .append (self ._generate_image_content (static_plot_path ))
300- else :
301- # Get the Network object
302- net = plot .create_and_save_pyvis_network (G , html_plot_file )
303- plot_content .append (self ._generate_plot_code (plot , html_plot_file ))
285+ plot_content .append (f"""fig_plotly.show()\n ```\n """ )
286+ elif plot .plot_type == r .PlotType .ALTAIR :
287+ plot_content .append (self ._generate_plot_code (plot ))
288+ if is_report_static :
289+ plot_content .append (f"""fig_altair.save("{ os .path .join (".." , static_plot_path )} ")\n ```\n """ )
290+ plot_content .append (self ._generate_image_content (static_plot_path ))
304291 else :
305- self .report .logger .warning (f"Unsupported interactive plot tool: { plot .int_visualization_tool } " )
306- except Exception as e :
307- self .report .logger .error (f"Error generating interactive plot content for { plot .title } : { str (e )} " )
308- raise
309-
310- elif plot .plot_type == r .PlotType .STATIC :
311- try :
312- plot_content .append (self ._generate_image_content (plot .file_path , width = 950 ))
313- except Exception as e :
314- self .report .logger .error (f"Error generating static plot content for { plot .title } : { str (e )} " )
315- raise
292+ plot_content .append (f"""fig_altair\n ```\n """ )
293+ elif plot .plot_type == r .PlotType .INTERACTIVE_NETWORK :
294+ G = plot .read_network ()
295+ num_nodes = G .number_of_nodes ()
296+ num_edges = G .number_of_edges ()
297+ plot_content .append (f'**Number of nodes:** { num_nodes } \n ' )
298+ plot_content .append (f'**Number of edges:** { num_edges } \n ' )
299+
300+ if is_report_static :
301+ plot .save_netwrok_image (G , static_plot_path , "png" )
302+ plot_content .append (self ._generate_image_content (static_plot_path ))
303+ else :
304+ # Get the Network object
305+ net = plot .create_and_save_pyvis_network (G , html_plot_file )
306+ plot_content .append (self ._generate_plot_code (plot , html_plot_file ))
307+ else :
308+ self .report .logger .warning (f"Unsupported plot type: { plot .plot_type } " )
309+ except Exception as e :
310+ self .report .logger .error (f"Error generating content for '{ plot .plot_type } ' plot '{ plot .id } ' '{ plot .title } ': { str (e )} " )
311+ raise
316312
317313 # Add caption if available
318314 if plot .caption :
@@ -344,13 +340,13 @@ def _generate_plot_code(self, plot, output_file = "") -> str:
344340 plot_data = plot_file.read()
345341 """
346342 # Add specific code for each visualization tool
347- if plot .int_visualization_tool == r .IntVisualizationTool .PLOTLY :
343+ if plot .plot_type == r .PlotType .PLOTLY :
348344 plot_code += """fig_plotly = pio.from_json(plot_data)
349345fig_plotly.update_layout(width=950, height=500)
350346 """
351- elif plot .int_visualization_tool == r .IntVisualizationTool .ALTAIR :
347+ elif plot .plot_type == r .PlotType .ALTAIR :
352348 plot_code += """fig_altair = alt.Chart.from_json(plot_data).properties(width=900, height=400)"""
353- elif plot .int_visualization_tool == r .IntVisualizationTool . PYVIS :
349+ elif plot .plot_type == r .PlotType . INTERACTIVE_NETWORK :
354350 plot_code = f"""<div style="text-align: center;">
355351<iframe src="{ os .path .join (".." , output_file )} " alt="{ plot .title } plot" width="800px" height="630px"></iframe>
356352</div>\n """
@@ -524,8 +520,8 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
524520 # Dictionary to hold the imports for each component type
525521 components_imports = {
526522 'plot' : {
527- r .IntVisualizationTool .ALTAIR : ['import altair as alt' ],
528- r .IntVisualizationTool .PLOTLY : ['import plotly.io as pio' ]
523+ r .PlotType .ALTAIR : ['import altair as alt' ],
524+ r .PlotType .PLOTLY : ['import plotly.io as pio' ]
529525 },
530526 'dataframe' : ['import pandas as pd' , 'from itables import show' , 'import dataframe_image as dfi' ],
531527 'markdown' : ['import IPython.display as display' ]
@@ -537,9 +533,9 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
537533
538534 # Add relevant imports based on component type and visualization tool
539535 if component_type == r .ComponentType .PLOT :
540- int_visualization_tool = getattr (component , 'int_visualization_tool ' , None )
541- if int_visualization_tool in components_imports ['plot' ]:
542- component_imports .extend (components_imports ['plot' ][int_visualization_tool ])
536+ plot_type = getattr (component , 'plot_type ' , None )
537+ if plot_type in components_imports ['plot' ]:
538+ component_imports .extend (components_imports ['plot' ][plot_type ])
543539 elif component_type == r .ComponentType .DATAFRAME :
544540 component_imports .extend (components_imports ['dataframe' ])
545541 elif component_type == r .ComponentType .MARKDOWN :
0 commit comments