validate_chart_code
Validate user-created chart code for compliance with PyCafe standards, ensuring correct function structure, imports, and data usage. Optionally open the validated chart link in a browser for direct review.
Instructions
Validate the chart code created by the user and optionally open the PyCafe link in a browser.
Args:
chart_config: A ChartPlan object with the chart configuration
data_info: Metadata for the dataset to be used in the chart
auto_open: Whether to automatically open the PyCafe link in a browser
Returns:
ValidationResults object with status and dashboard details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| auto_open | No | ||
| chart_config | Yes | ||
| data_info | Yes |
Implementation Reference
- The primary handler function for the 'validate_chart_code' tool. It is decorated with @mcp.tool() for registration. Validates the ChartPlan configuration, generates dashboard code and PyCafe URL if data is remote, optionally opens in browser, and returns validation results.@mcp.tool() def validate_chart_code( chart_config: ChartPlan = Field(description="A ChartPlan object with the chart configuration"), data_info: DFMetaData = Field(description="Metadata for the dataset to be used in the chart"), auto_open: bool = Field(default=True, description="Whether to automatically open the PyCafe link in a browser"), ) -> ValidateResults: """Validate the chart code created by the user and optionally open the PyCafe link in a browser. Returns: ValidationResults object with status and dashboard details """ Vizro._reset() try: chart_plan_obj = ChartPlan.model_validate(chart_config) except ValidationError as e: return ValidateResults( valid=False, message=f"Validation Error: {e!s}", python_code="", pycafe_url=None, browser_opened=False, ) else: dashboard_code = chart_plan_obj.get_dashboard_template(data_info=data_info) # Generate PyCafe URL if all data is remote pycafe_url = create_pycafe_url(dashboard_code) if data_info.file_location_type == "remote" else None browser_opened = False if auto_open and pycafe_url: try: browser_opened = webbrowser.open(pycafe_url) except Exception: browser_opened = False return ValidateResults( valid=True, message="Chart only dashboard created successfully!", python_code=chart_plan_obj.get_chart_code(vizro=True), pycafe_url=pycafe_url, browser_opened=browser_opened, ) finally: Vizro._reset()
- vizro-mcp/src/vizro_mcp/server.py:324-324 (registration)The @mcp.tool() decorator registers the validate_chart_code function as an MCP tool.@mcp.tool()
- Pydantic Field definitions providing the input schema for the tool, including ChartPlan, DFMetaData, and auto_open parameters.chart_config: ChartPlan = Field(description="A ChartPlan object with the chart configuration"), data_info: DFMetaData = Field(description="Metadata for the dataset to be used in the chart"), auto_open: bool = Field(default=True, description="Whether to automatically open the PyCafe link in a browser"), ) -> ValidateResults: