1616import json
1717import logging
1818import os
19+ from io import StringIO
1920from pathlib import Path
20- from typing import Any , Dict , Iterable , List , Optional , Union
21+ from typing import Any , Iterable
2122
2223import IPython
2324import pandas as pd
@@ -58,7 +59,7 @@ def __init__(self, access_token=_ACCESS_TOKEN):
5859 self ._access_token = access_token
5960 self ._auth_header = {"Authorization" : f"Bearer { access_token } " }
6061
61- def account_info (self ) -> Union [ Dict [ Any , Any ], None , Any ] :
62+ def account_info (self ) -> dict [ Any , Any ] | None | Any :
6263 """Access your account information.
6364
6465 Returns
@@ -138,8 +139,8 @@ def create_chart(
138139 data : pd .DataFrame | str | None = None ,
139140 folder_id : str = "" ,
140141 organization_id : str = "" ,
141- metadata : Optional [ Dict [ Any , Any ]] = None ,
142- ) -> Union [ Dict [ Any , Any ], None , Any ] :
142+ metadata : dict [ Any , Any ] | None = None ,
143+ ) -> dict [ Any , Any ] | None | Any :
143144 """Creates a new Datawrapper chart, table or map.
144145
145146 You can pass a pandas DataFrame as a `data` argument to upload data.
@@ -220,7 +221,7 @@ def update_description(
220221 number_append : str = "" ,
221222 number_format : str = "-" ,
222223 number_divisor : int = 0 ,
223- ) -> Union [ Any , None ] :
224+ ) -> Any | None :
224225 """Update a chart's description.
225226
226227 Parameters
@@ -278,7 +279,7 @@ def update_description(
278279 logger .error ("Couldn't update chart." )
279280 return None
280281
281- def publish_chart (self , chart_id : str , display : bool = True ) -> Union [ Any , None ] :
282+ def publish_chart (self , chart_id : str , display : bool = True ) -> Any | None :
282283 """Publishes a chart, table or map.
283284
284285 Parameters
@@ -311,7 +312,7 @@ def publish_chart(self, chart_id: str, display: bool = True) -> Union[Any, None]
311312
312313 def chart_properties (
313314 self , chart_id : str
314- ) -> Union [ Dict [ Any , Any ], None , Any , Iterable [Any ] ]:
315+ ) -> dict [ Any , Any ] | None | Any | Iterable [Any ]:
315316 """Retrieve information of a specific chart, table or map.
316317
317318 Parameters
@@ -356,14 +357,21 @@ def chart_data(self, chart_id: str):
356357 )
357358
358359 # Check if the request was successful
359- assert response .ok , "Make sure you have the right id and authorization credentials (access_token)."
360-
361- # Return the data
362- return response .json ()
360+ assert (
361+ response .ok
362+ ), "Make sure you have the right id and authorization credentials (access_token)."
363+
364+ # Return the data as json if the mimetype is json
365+ if "json" in response .headers ["content-type" ]:
366+ return response .json ()
367+ # If it's a csv, read the text into a dataframe
368+ elif "text/csv" in response .headers ["content-type" ]:
369+ return pd .read_csv (StringIO (response .text ))
370+ # Otherwise just return the text
371+ else :
372+ return response .text
363373
364- def update_metadata (
365- self , chart_id : str , properties : Dict [Any , Any ]
366- ) -> Union [Any , None ]:
374+ def update_metadata (self , chart_id : str , properties : dict [Any , Any ]) -> Any | None :
367375 """Update a chart, table, or map's metadata.
368376
369377 Example: https://developer.datawrapper.de/docs/creating-a-chart-new#edit-colors
@@ -404,7 +412,7 @@ def update_chart(
404412 language : str = "" ,
405413 folder_id : str = "" ,
406414 organization_id : str = "" ,
407- ) -> Union [ Any , None ] :
415+ ) -> Any | None :
408416 """Updates a chart's title, theme, type, language, or location (folder/organization).
409417
410418 Parameters
@@ -473,9 +481,7 @@ def display_chart(self, chart_id: str) -> IPython.display.HTML:
473481
474482 return HTML (_iframe_code )
475483
476- def get_iframe_code (
477- self , chart_id : str , responsive : bool = False
478- ) -> Union [str , Any ]:
484+ def get_iframe_code (self , chart_id : str , responsive : bool = False ) -> str | Any :
479485 """Returns a chart, table, or map's iframe embed code.
480486
481487 Parameters
@@ -516,7 +522,7 @@ def export_chart(
516522 output : str = "png" ,
517523 filepath : str = "./image.png" ,
518524 display : bool = False ,
519- ) -> Union [ Any , None ] :
525+ ) -> Any | None :
520526 """Exports a chart, table, or map.
521527
522528 Parameters
@@ -564,7 +570,7 @@ def export_chart(
564570 "zoom" : zoom ,
565571 "scale" : scale ,
566572 "borderWidth" : border_width ,
567- "transparent" : transparent
573+ "transparent" : transparent ,
568574 }
569575
570576 _header = self ._auth_header
@@ -595,7 +601,7 @@ def export_chart(
595601 logger .error (msg )
596602 raise Exception (msg )
597603
598- def get_folders (self ) -> Union [ Dict [ Any , Any ], None , Any ] :
604+ def get_folders (self ) -> dict [ Any , Any ] | None | Any :
599605 """Get a list of folders in your Datawrapper account.
600606
601607 Returns
@@ -616,7 +622,7 @@ def get_folders(self) -> Union[Dict[Any, Any], None, Any]:
616622 )
617623 return None
618624
619- def move_chart (self , chart_id : str , folder_id : str ) -> Union [ Any , None ] :
625+ def move_chart (self , chart_id : str , folder_id : str ) -> Any | None :
620626 """Moves a chart, table, or map to a specified folder.
621627
622628 Parameters
@@ -677,7 +683,7 @@ def get_charts(
677683 limit : int = 25 ,
678684 folder_id : str = "" ,
679685 team_id : str = "" ,
680- ) -> Union [ None , List [Any ] ]:
686+ ) -> None | list [Any ]:
681687 """Retrieves a list of charts by User
682688
683689 Parameters
0 commit comments