Skip to content

Commit 8d11b0f

Browse files
committed
refactor: update chart creation to return chart object for method chaining
1 parent b3f0d40 commit 8d11b0f

8 files changed

Lines changed: 24 additions & 37 deletions

File tree

docs/user-guide/chart-operations.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Working with chart objects
22

3-
This guide tours the key operations available on all of our object-oriented chart types.
3+
This guide tours the key operations available on all charts based on our object-oriented classes.
44

55
## Creating a new chart
66

@@ -16,19 +16,20 @@ data = pd.DataFrame(
1616
}
1717
)
1818

19-
# Create a chart
19+
# Configure the chart object
2020
chart = dw.BarChart(
2121
title="Most Popular Programming Languages 2024",
2222
data=data,
2323
value_label_format=dw.NumberFormat.ONE_DECIMAL,
2424
)
2525

26+
# Create it by sending to Datawrapper
2627
chart.create()
2728
```
2829

2930
## Getting an existing chart
3031

31-
You can retrieve an existing chart by using `get_chart` with the chart ID:
32+
You can retrieve an existing chart with the ID, which is found in its URL.
3233

3334
```python
3435
# Retrieve an existing chart
@@ -41,8 +42,6 @@ print(existing_chart.chart_id)
4142

4243
## Updating an existing chart
4344

44-
After creating or retrieving a chart, you can modify its properties and update it:
45-
4645
```python
4746
# Modify the chart properties
4847
chart.title = "Programming Language Popularity - Updated"
@@ -57,7 +56,7 @@ new_data = pd.DataFrame(
5756
)
5857
chart.data = new_data
5958

60-
# Push the changes to Datawrapper (returns self for method chaining)
59+
# This will send the updates to Datawrapper
6160
chart.update()
6261
```
6362

@@ -66,7 +65,6 @@ chart.update()
6665
Once your chart is ready, publish it to make it publicly accessible:
6766

6867
```python
69-
# Publish the chart (returns self for method chaining)
7068
chart.publish()
7169

7270
# Chain with other operations
@@ -75,8 +73,6 @@ chart.create().publish()
7573

7674
## Exporting a chart
7775

78-
Export your chart as an SVG, PNG, or PDF file:
79-
8076
```python
8177
# Export as PNG (default)
8278
chart.export(filepath="chart.png")
@@ -90,14 +86,9 @@ chart.export(
9086
)
9187
```
9288

93-
The export method supports various other options for customizing the output format and dimensions.
94-
9589
## Duplicating a chart
9690

97-
Create an editable copy of your chart:
98-
9991
```python
100-
# Create a duplicate
10192
duplicate_chart = chart.duplicate()
10293

10394
# The duplicate is a new chart with a different ID
@@ -111,10 +102,7 @@ duplicate_chart.update()
111102

112103
## Deleting a chart
113104

114-
Remove a chart from Datawrapper:
115-
116105
```python
117-
# Delete the chart
118106
success = chart.delete()
119107

120108
if success:
@@ -136,22 +124,17 @@ Get the HTML iframe embed code for your chart:
136124
```python
137125
# Get standard iframe code
138126
iframe_code = chart.get_iframe_code()
139-
print(iframe_code)
140127

141128
# Get responsive iframe code
142129
responsive_iframe = chart.get_iframe_code(responsive=True)
143-
print(responsive_iframe)
144130
```
145131

146132
## Getting png URL
147133

148134
Get the fallback image URL for use in noscript tags:
149135

150136
```python
151-
# Get the PNG URL
152137
png_url = chart.get_png_url()
153-
print(f"PNG fallback: {png_url}")
154138

155-
# Use in HTML noscript tags
156139
html = f'<noscript><img src="{png_url}" alt="Chart" /></noscript>'
157140
```

tests/functional/test_chart_factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_get_chart_line_chart():
3232
mock_client.get_chart.return_value = mock_metadata
3333

3434
with (
35+
patch.dict("os.environ", {}, clear=True), # Clear environment variables
3536
patch("datawrapper.Datawrapper", return_value=mock_client),
3637
patch.object(LineChart, "get") as mock_line_get,
3738
):

tests/integration/test_area_chart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ def test_round_trip_create_and_get(self):
350350

351351
# Create the chart
352352
with patch.object(original_chart, "_get_client", return_value=mock_client):
353-
chart_id = original_chart.create(access_token="test-token")
353+
original_chart.create(access_token="test-token")
354+
chart_id = original_chart.chart_id
354355

355356
# Now fetch it back
356357
serialized = original_chart.serialize_model()

tests/integration/test_arrow_chart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def test_round_trip_create_and_get(self):
419419

420420
# Create the chart
421421
with patch.object(original_chart, "_get_client", return_value=mock_client):
422-
chart_id = original_chart.create(access_token="test-token")
422+
original_chart.create(access_token="test-token")
423+
chart_id = original_chart.chart_id
423424

424425
# Now fetch it back
425426
serialized = original_chart.serialize_model()

tests/integration/test_base_api_integration.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ def test_create_chart_success(self):
7777
mock_client._CHARTS_URL = "https://api.datawrapper.de/v3/charts"
7878

7979
with patch.object(chart, "_get_client", return_value=mock_client):
80-
chart_id = chart.create(access_token="test-token")
80+
chart = chart.create(access_token="test-token")
8181

82-
assert chart_id == "test-chart-id"
8382
assert chart.chart_id == "test-chart-id"
8483

8584
# Verify create_chart was called with correct parameters
@@ -128,9 +127,9 @@ def test_update_chart_success(self):
128127
mock_client._CHARTS_URL = "https://api.datawrapper.de/v3/charts"
129128

130129
with patch.object(chart, "_get_client", return_value=mock_client):
131-
chart_id = chart.update(access_token="test-token")
130+
chart = chart.update(access_token="test-token")
132131

133-
assert chart_id == "existing-chart-id"
132+
assert chart.chart_id == "existing-chart-id"
134133

135134
# Verify update_chart was called with correct parameters
136135
mock_client.update_chart.assert_called_once()
@@ -182,9 +181,9 @@ def test_create_with_list_data(self):
182181
mock_client._CHARTS_URL = "https://api.datawrapper.de/v3/charts"
183182

184183
with patch.object(chart, "_get_client", return_value=mock_client):
185-
chart_id = chart.create(access_token="test-token")
184+
chart = chart.create(access_token="test-token")
186185

187-
assert chart_id == "test-chart-id"
186+
assert chart.chart_id == "test-chart-id"
188187

189188
# Verify create_chart was called for chart creation
190189
mock_client.create_chart.assert_called_once()
@@ -219,16 +218,15 @@ def test_full_workflow_create_then_update(self):
219218

220219
with patch.object(chart, "_get_client", return_value=mock_client):
221220
# Create the chart
222-
chart_id = chart.create(access_token="test-token")
223-
assert chart_id == "workflow-chart-id"
221+
chart = chart.create(access_token="test-token")
224222
assert chart.chart_id == "workflow-chart-id"
225223

226224
# Update the chart
227225
chart.title = "Updated Title"
228226
chart.data = pd.DataFrame({"x": [5, 6], "y": [7, 8]})
229227

230-
updated_id = chart.update(access_token="test-token")
231-
assert updated_id == "workflow-chart-id"
228+
updated = chart.update(access_token="test-token")
229+
assert updated.chart_id == "workflow-chart-id"
232230

233231
# Verify both create and update were called
234232
assert mock_client.create_chart.call_count == 1 # Chart creation

tests/integration/test_chart_get_method.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ def test_round_trip_create_and_get(self):
587587

588588
# Create the chart
589589
with patch.object(original_chart, "_get_client", return_value=mock_client):
590-
chart_id = original_chart.create(access_token="test-token")
590+
original_chart.create(access_token="test-token")
591+
chart_id = original_chart.chart_id
591592

592593
# Now fetch it back
593594
serialized = original_chart.serialize_model()

tests/integration/test_column_chart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ def test_round_trip_create_and_get(self):
485485

486486
# Create the chart
487487
with patch.object(original_chart, "_get_client", return_value=mock_client):
488-
chart_id = original_chart.create(access_token="test-token")
488+
original_chart.create(access_token="test-token")
489+
chart_id = original_chart.chart_id
489490

490491
# Now fetch it back
491492
serialized = original_chart.serialize_model()

tests/integration/test_line_chart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ def test_round_trip_create_and_get(self):
432432

433433
# Create the chart
434434
with patch.object(original_chart, "_get_client", return_value=mock_client):
435-
chart_id = original_chart.create(access_token="test-token")
435+
original_chart.create(access_token="test-token")
436+
chart_id = original_chart.chart_id
436437

437438
# Now fetch it back
438439
serialized = original_chart.serialize_model()

0 commit comments

Comments
 (0)