Spaces:
Running
Running
File size: 2,193 Bytes
7114af0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
"""Handler for updating Datawrapper charts."""
import json
from datawrapper import get_chart
from mcp.types import TextContent
from ..utils import get_api_token, json_to_dataframe
async def update_chart(arguments: dict) -> list[TextContent]:
"""Update an existing chart's data or configuration."""
api_token = get_api_token()
chart_id = arguments["chart_id"]
# Get chart using factory function - returns correct Pydantic class instance
chart = get_chart(chart_id, access_token=api_token)
# Update data if provided
if "data" in arguments:
df = json_to_dataframe(arguments["data"])
chart.data = df
# Update config if provided
if "chart_config" in arguments:
# Directly set attributes on the chart instance
# Pydantic will validate each assignment automatically due to validate_assignment=True
try:
# Build a mapping of aliases to field names
alias_to_field = {}
for field_name, field_info in chart.model_fields.items():
# Add the field name itself
alias_to_field[field_name] = field_name
# Add any aliases
if field_info.alias:
alias_to_field[field_info.alias] = field_name
for key, value in arguments["chart_config"].items():
# Convert alias to field name if needed
field_name = alias_to_field.get(key, key)
setattr(chart, field_name, value)
except Exception as e:
return [
TextContent(
type="text",
text=f"Invalid chart configuration: {str(e)}\n\n"
f"Use get_chart_schema to see the valid schema for this chart type. "
f"Only high-level Pydantic fields are accepted.",
)
]
# Update using Pydantic instance method
chart.update(access_token=api_token)
result = {
"chart_id": chart.chart_id,
"message": "Chart updated successfully!",
"edit_url": chart.get_editor_url(),
}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
|