Skip to content

Commit

Permalink
Merge pull request #380 from palewire/fork-chart
Browse files Browse the repository at this point in the history
Add fork_chart method
  • Loading branch information
chekos authored Oct 14, 2023
2 parents f01806d + 965f6e8 commit 58a6edd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
31 changes: 31 additions & 0 deletions datawrapper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion tests/test_datawrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
Expand Down

0 comments on commit 58a6edd

Please sign in to comment.