Spaces:
Running
Running
File size: 1,495 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 |
"""Handler for exporting Datawrapper charts."""
import base64
from datawrapper import get_chart
from mcp.types import ImageContent
from ..utils import get_api_token
async def export_chart_png(arguments: dict) -> list[ImageContent]:
"""Export a chart as PNG and return it as inline image."""
api_token = get_api_token()
chart_id = arguments["chart_id"]
# Get chart using factory function
chart = get_chart(chart_id, access_token=api_token)
# Build export parameters
export_params = {}
if "width" in arguments:
export_params["width"] = arguments["width"]
if "height" in arguments:
export_params["height"] = arguments["height"]
if "plain" in arguments:
export_params["plain"] = arguments["plain"]
if "zoom" in arguments:
export_params["zoom"] = arguments["zoom"]
if "transparent" in arguments:
export_params["transparent"] = arguments["transparent"]
if "border_width" in arguments:
export_params["borderWidth"] = arguments["border_width"]
if "border_color" in arguments:
export_params["borderColor"] = arguments["border_color"]
# Export PNG using Pydantic instance method
png_bytes = chart.export_png(access_token=api_token, **export_params)
# Encode to base64
base64_data = base64.b64encode(png_bytes).decode("utf-8")
return [
ImageContent(
type="image",
data=base64_data,
mimeType="image/png",
)
]
|