Skip to content

Commit c0bcce6

Browse files
committed
Correctly handle config python strings and add short URL example with function
1 parent b390f8e commit c0bcce6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from quickchart import QuickChart
2+
3+
qc = QuickChart()
4+
qc.config = '''{
5+
type: 'bar',
6+
data: {
7+
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
8+
datasets: [{
9+
label: 'Users',
10+
data: [50, 60, 70, 180]
11+
}, {
12+
label: 'Revenue',
13+
data: [100, 200, 300, 400]
14+
}]
15+
},
16+
options: {
17+
scales: {
18+
yAxes: [{
19+
ticks: {
20+
callback: (val) => {
21+
return val + 'k';
22+
}
23+
}
24+
}]
25+
}
26+
}
27+
}'''
28+
29+
print(qc.get_short_url())
30+
#
31+
# Example output (note that this shortened URL is now expired and will not display a chart):
32+
#
33+
# https://quickchart.io/chart/render/f-b4bf9221-0499-4bc6-b1ae-6f7c78be9d93
34+
#

quickchart.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_url(self):
2525
if not self.is_valid():
2626
raise RuntimeError('You must set the `config` attribute before generating a url')
2727
params = {
28-
'c': json.dumps(self.config),
28+
'c': json.dumps(self.config) if type(self.config) == dict else self.config,
2929
'w': self.width,
3030
'h': self.height,
3131
'bkg': self.background_color,
@@ -43,7 +43,7 @@ def get_short_url(self):
4343
raise RuntimeError('Could not find `requests` dependency')
4444

4545
postdata = {
46-
'chart': json.dumps(self.config),
46+
'chart': json.dumps(self.config) if type(self.config) == dict else self.config,
4747
'width': self.width,
4848
'height': self.height,
4949
'backgroundColor': self.background_color,

0 commit comments

Comments
 (0)