diff --git a/vizro-ai/examples/example_dashboard.py b/vizro-ai/examples/example_dashboard.py new file mode 100644 index 000000000..98e919018 --- /dev/null +++ b/vizro-ai/examples/example_dashboard.py @@ -0,0 +1,31 @@ +from vizro import Vizro +from vizro_ai import VizroAI +import vizro.plotly.express as px +import pandas as pd +import vizro.models as vm +import plotly.express as px + + +Vizro._reset() +vizro_ai = VizroAI(model="gpt-4-turbo") + +gapminder_data = px.data.gapminder() +tips_data = px.data.tips() + +dfs=[gapminder_data, tips_data] +input_text = ( + "Create a dashboard that displays the Gapminder dataset and the tips dataset. " + "page1 displays the Gapminder dataset. use a graph to display the data summary. " + "add a filter to filter by year. " + "Use a card to explain what Gapminder dataset is about. " + "The card should only take 1/6 of the whole page. " + "The rest of the page should be the graph or table. Don't create empty space." + "page2 displays the tips dataset. use two different charts to help me understand the data " + "distributions. one chart should be a bar chart and the other should be a scatter plot. " + "first chart is on the left and the second chart is on the right. " + "add a filter to filter one of the categorical columns." + ) + +if __name__ == "__main__": + res = vizro_ai.dashboard(dfs=dfs, user_input=input_text, return_elements=True) + vizro_ai.run_dashboard(res.dashboard, res.metadata) \ No newline at end of file diff --git a/vizro-ai/src/vizro_ai/_vizro_ai.py b/vizro-ai/src/vizro_ai/_vizro_ai.py index 8592d9139..c77d50064 100644 --- a/vizro-ai/src/vizro_ai/_vizro_ai.py +++ b/vizro-ai/src/vizro_ai/_vizro_ai.py @@ -10,7 +10,7 @@ from vizro_ai.chains._llm_models import _get_llm_model, _get_model_name from vizro_ai.components import GetCodeExplanation, GetDebugger from vizro_ai.dashboard.graph.dashboard_creation import _create_and_compile_graph -from vizro_ai.dashboard.utils import DashboardOutputs, _dashboard_code +from vizro_ai.dashboard.utils import DashboardOutputs, DfMetadata, _dashboard_code, _run_dashboard from vizro_ai.task_pipeline._pipeline_manager import PipelineManager from vizro_ai.utils.helper import ( DebugFailure, @@ -160,6 +160,11 @@ def plot( # pylint: disable=too-many-arguments # noqa: PLR0913 return vizro_plot if return_elements else vizro_plot.figure + @staticmethod + def run_dashboard(dashboard: vm.Dashboard, df_metadata: DfMetadata) -> None: + """Run the dashboard.""" + _run_dashboard(dashboard=dashboard, df_metadata=df_metadata) + def dashboard( self, dfs: List[pd.DataFrame], @@ -192,10 +197,11 @@ def dashboard( config=config, ) dashboard = message_res["dashboard"] + metadata = message_res["df_metadata"] if return_elements: code = _dashboard_code(dashboard) # TODO: `_dashboard_code` to be implemented - dashboard_output = DashboardOutputs(dashboard=dashboard, code=code) + dashboard_output = DashboardOutputs(dashboard=dashboard, code=code, metadata=metadata) return dashboard_output else: return dashboard diff --git a/vizro-ai/src/vizro_ai/dashboard/response_models/controls.py b/vizro-ai/src/vizro_ai/dashboard/response_models/controls.py index a5d950cdf..d5d9f4177 100644 --- a/vizro-ai/src/vizro_ai/dashboard/response_models/controls.py +++ b/vizro-ai/src/vizro_ai/dashboard/response_models/controls.py @@ -94,13 +94,21 @@ def create(self, model, available_components, df_metadata): try: if self.control_type == "Filter": - return _create_filter( + res = _create_filter( filter_prompt=filter_prompt, model=model, df_cols=_df_cols, df_schema=_df_schema, available_components=available_components, ) + if res.targets == []: + logger.warning( + f"Filter control failed to create, " + f"related user input: `{self.control_description}`." + f"This might be due to the filter target is not found in the available components. " + "returning default values." + ) + return None else: logger.warning(f"Control type {self.control_type} not recognized.") return None diff --git a/vizro-ai/src/vizro_ai/dashboard/utils.py b/vizro-ai/src/vizro_ai/dashboard/utils.py index f0c87659b..049b63da9 100644 --- a/vizro-ai/src/vizro_ai/dashboard/utils.py +++ b/vizro-ai/src/vizro_ai/dashboard/utils.py @@ -14,7 +14,10 @@ "from vizro.models.types import capture\n" "import plotly.graph_objects as go\n" "from vizro.tables import dash_ag_grid\n" - "import vizro.models as vm\n" + "from vizro.models import AgGrid, Card, Dashboard, Filter, Layout, Page, Graph\n" + "from vizro.managers import data_manager\n" + "from vizro import Vizro\n" + "import pandas as pd\n" ) @@ -55,6 +58,7 @@ class DashboardOutputs: code: str dashboard: vm.Dashboard + metadata: DfMetadata def _execute_step(pbar: tsd.tqdm, description: str, value: Any) -> Any: @@ -70,3 +74,14 @@ def _dashboard_code(dashboard: vm.Dashboard) -> str: # TODO: use black or ruff to format the code # formatted_code = black.format_str(dashboard_code_str, mode=black.Mode()) return dashboard_code_str + + +def _run_dashboard(dashboard: vm.Dashboard, df_metadata: DfMetadata) -> None: + """Run the dashboard.""" + from vizro import Vizro + from vizro.managers import data_manager + + for name, metadata in df_metadata.metadata.items(): + data_manager[name] = metadata.df + + Vizro().build(dashboard).run()