Using data tables to store plotly charts

Thanks. I must admit I didn’t really understand what you meant (kwargs threw me) but it seems that chatGPT did! So after a few trials we got the following code to work (in case it is of any use to anyone else). Now to apply it to the real world…

‘’’
class Charts(ChartsTemplate):
def init(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)

    from plotly import graph_objects as go
    
    # Sample data
    example_data = [
        go.Scatter(
            x=[1, 2, 3],
            y=[3, 1, 6],
            marker=dict(color='rgb(16, 32, 77)')
        ),
        go.Bar(
            x=[1, 2, 3],
            y=[3, 1, 6],
            name='Bar Chart Example'
        )
    ]
    
    # Save it in the table - add new row
    in_row = app_tables.chart_data.add_row(chart_name="Example", chart_data=example_data)
    
    # Retrieve from the table
    out_row = app_tables.chart_data.get(chart_name="Example")
    
    # Unwrap and extract the chart data
    chart_data = out_row['chart_data']
    
    # Convert out_row['chart_data'] into the same format as example_data
    converted_data = []
    for item in chart_data:
        chart_type = item['type'].split('.')[-1]  # Extracting chart type
        chart_args = item['value']
        # Unwrap the wrapped objects
        chart_args = {key: value['value'] if isinstance(value, dict) and 'value' in value else value for key, value in chart_args.items()}
        # Construct Plotly graph objects based on chart type and args
        if chart_type == 'Scatter':
            chart_obj = go.Scatter(**chart_args)
        elif chart_type == 'Bar':
            chart_obj = go.Bar(**chart_args)
        else:
            raise ValueError(f"Unsupported chart type: {chart_type}")
        converted_data.append(chart_obj)
    
    # Set the figure for plot_1
    self.plot_1.figure = converted_data
    
    # Set the figure for plot_2 (for comparison)
    self.plot_2.figure = example_data

‘’’