diff --git a/datawrapper/__main__.py b/datawrapper/__main__.py index f79de68..d331113 100644 --- a/datawrapper/__main__.py +++ b/datawrapper/__main__.py @@ -844,6 +844,37 @@ def copy_chart(self, chart_id: str) -> dict[Any, Any]: logger.error(msg) raise Exception(msg) + def fork_chart(self, chart_id: str) -> str: + """Fork a chart, table, or map and create an editable copy. + + Parameters + ---------- + chart_id : str + ID of chart, table, or map. + + Returns + ------- + dict + A dictionary containing the information of the chart, table, or map. + """ + _header = self._auth_header + _header["accept"] = "*/*" + + url = f"{self._CHARTS_URL}/{chart_id}/fork" + response = r.post( + url=url, + headers=_header + ) + + if response.ok: + fork_id = response.json()["id"] + logger.debug(f"Chart {chart_id} copied to {fork_id}") + return fork_id + else: + msg = "Chart could not be forked. If it's a chart you created, you should trying copying it instead." + logger.error(msg) + raise Exception(msg) + def delete_chart(self, chart_id: str) -> r.Response.content: # type: ignore """Deletes a specified chart, table or map. diff --git a/tests/test_datawrapper.py b/tests/test_datawrapper.py index d454185..4d0b8e3 100644 --- a/tests/test_datawrapper.py +++ b/tests/test_datawrapper.py @@ -14,6 +14,16 @@ def test_get_folders(): dw = Datawrapper() dw.get_folders() +def test_fork(): + """Test the fork_chart method.""" + dw = Datawrapper() + # For the test, we will fork a random + # chart from the Datawrapper "river" + # of open-source material. + source_id = "dZntB" + fork = dw.fork_chart(source_id) + assert isinstance(fork, dict) + assert source_id != fork["id"] def test_copy(): """Test the copy_chart method.""" @@ -26,7 +36,6 @@ def test_copy(): assert isinstance(copy_info, dict) assert chart_info["title"] == copy_info["title"] - def test_usage(): """Test creating and updating charts with the same code as our example notebook.""" # Connect