Skip to content

Commit 23cea9d

Browse files
committed
add get_bytes and to_file
1 parent 6befec3 commit 23cea9d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ Uses the quickchart.io web service to create a fixed-length chart URL that displ
8888

8989
Note that short URLs expire after a few days for users of the free service. You can [subscribe](https://quickchart.io/pricing/) to keep them around longer.
9090

91+
## Other functionality
92+
93+
### get_bytes()
94+
95+
Returns the bytes representing the chart image.
96+
97+
### to_file(path: str)
98+
99+
Writes the chart image to a file path.
100+
91101
## More examples
92102

93103
Checkout the `examples` directory to see other usage.

quickchart.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_url(self):
3636
params['key'] = self.key
3737
return 'https://quickchart.io/chart?%s' % urlencode(params)
3838

39-
def get_short_url(self):
39+
def _post(self, path):
4040
try:
4141
import requests
4242
except:
@@ -52,10 +52,24 @@ def get_short_url(self):
5252
}
5353
if self.key:
5454
postdata['key'] = self.key
55-
resp = requests.post('https://quickchart.io/chart/create', json=postdata)
55+
resp = requests.post('https://quickchart.io/chart', json=postdata)
5656
if resp.status_code != 200:
5757
raise RuntimeError('Invalid response code from chart creation endpoint')
58+
return resp
59+
60+
def get_short_url(self):
61+
resp = self._post('https://quickchart.io/chart/create')
5862
parsed = json.loads(resp.text)
5963
if not parsed['success']:
6064
raise RuntimeError('Failure response status from chart creation endpoint')
6165
return parsed['url']
66+
67+
def get_bytes(self):
68+
resp = self._post('https://quickchart.io/chart')
69+
return response.content
70+
71+
def to_file(self, path):
72+
content = self.get_bytes()
73+
with open(path, 'wb') as f:
74+
f.write(response.content)
75+

0 commit comments

Comments
 (0)