Skip to content

Commit

Permalink
add dashboard launch method
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyielia committed Jul 19, 2024
1 parent def3b77 commit b955edb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
31 changes: 31 additions & 0 deletions vizro-ai/examples/example_dashboard.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 8 additions & 2 deletions vizro-ai/src/vizro_ai/_vizro_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
10 changes: 9 additions & 1 deletion vizro-ai/src/vizro_ai/dashboard/response_models/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion vizro-ai/src/vizro_ai/dashboard/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)


Expand Down Expand Up @@ -55,6 +58,7 @@ class DashboardOutputs:

code: str
dashboard: vm.Dashboard
metadata: DfMetadata


def _execute_step(pbar: tsd.tqdm, description: str, value: Any) -> Any:
Expand All @@ -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()

0 comments on commit b955edb

Please sign in to comment.