Skip to content

Commit 69ef41d

Browse files
authoredAug 6, 2018
Chart data files (#3130)
Fix #3129 by providing support for JSON/YAML files in chart shortcodes and directives
1 parent 42e6fd6 commit 69ef41d

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changes since Beta 2
66
Features
77
--------
88

9+
* New data_file option for chart shortcode and directive (Issue #3129)
910
* Show the filename of the missing file when ``nikola serve`` can't
1011
find a file (i.e. when an 404 error occurs).
1112
* Better error messages for JSON download failures in ``nikola

‎docs/manual.rst

+29-2
Original file line numberDiff line numberDiff line change
@@ -1256,14 +1256,39 @@ chart
12561256

12571257
.. code:: text
12581258
1259-
{{% raw %}}{{% chart Bar title='Browser usage evolution (in %)' %}}
1260-
x_labels='["2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012"]'%}}
1259+
{{% raw %}}{{% chart Bar title='Browser usage evolution (in %)'
1260+
x_labels='["2002","2003","2004","2005","2006","2007"]' %}}
12611261
'Firefox', [None, None, 0, 16.6, 25, 31]
12621262
'Chrome', [None, None, None, None, None, None]
12631263
'IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6]
12641264
'Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4]
12651265
{{% /chart %}}{{% /raw %}}
12661266

1267+
Additionally, you can use a file_data argument which can point to a JSON or YAML file, and will be used for both arguments and data.
1268+
Example:
1269+
1270+
.. code:: json
1271+
1272+
{
1273+
"x_labels": ["2002","2003","2004","2005","2006","2007"],
1274+
"data": {
1275+
"Firefox": [null, null, 0, 16.6, 25, 31],
1276+
"Chrome": [null, null, null, null, null, null],
1277+
"IE": [85.8, 84.6, 84.7, 74.5, 66, 58.6],
1278+
"Others": [14.2, 15.4, 15.3, 8.9, 9, 10.4]
1279+
}
1280+
}
1281+
1282+
Which can be used like this:
1283+
1284+
.. code:: text
1285+
1286+
{{% raw %}}{{% chart Bar title='Browser usage evolution (in %)' data_file="posts/browsers.json" %}}
1287+
{{% /chart %}}
1288+
{{% /raw %}}
1289+
1290+
If the data or any option is available in both the ``data_file`` and the document, the document has priority.
1291+
12671292
doc
12681293
Will link to a document in the page, see `Doc role for details
12691294
<#doc>`__. Example:
@@ -2663,6 +2688,8 @@ You can use any option described in `the pygal docs <http://pygal.org/en/stable/
26632688
Finally, the content of the directive is the actual data, in the form of a label and
26642689
a list of values, one series per line.
26652690

2691+
You can also specify a ``:data_file:`` option as described in the documentation for the chart shortcut.
2692+
26662693
Doc
26672694
~~~
26682695

‎nikola/plugins/compile/rest/chart.py

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Chart(Directive):
7272
"classes": directives.unchanged,
7373
"css": directives.unchanged,
7474
"defs": directives.unchanged,
75+
"data_file": directives.unchanged,
7576
"disable_xml_declaration": directives.unchanged,
7677
"dots_size": directives.unchanged,
7778
"dynamic_print_values": directives.unchanged,

‎nikola/plugins/shortcode/chart.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
pygal = None # NOQA
3434

3535
from nikola.plugin_categories import ShortcodePlugin
36-
from nikola.utils import req_missing
36+
from nikola.utils import req_missing, load_data
3737

3838
_site = None
3939

@@ -50,18 +50,30 @@ def handler(self, chart_type, **_options):
5050
['pygal'], 'use the Chart directive', optional=True)
5151
return '<div class="text-error">{0}</div>'.format(msg)
5252
options = {}
53-
data = _options.pop('data')
53+
chart_data = []
5454
_options.pop('post', None)
5555
_options.pop('site')
56-
if 'style' in _options:
57-
style_name = _options.pop('style')
58-
else:
59-
style_name = 'BlueStyle'
56+
data = _options.pop('data')
57+
58+
for line in data.splitlines():
59+
line = line.strip()
60+
if line:
61+
chart_data.append(literal_eval('({0})'.format(line)))
62+
if 'data_file' in _options:
63+
options = load_data(_options['data_file'])
64+
_options.pop('data_file')
65+
if not chart_data: # If there is data in the document, it wins
66+
for k, v in options.pop('data', {}).items():
67+
chart_data.append((k, v))
68+
69+
options.update(_options)
70+
71+
style_name = options.pop('style', 'BlueStyle')
6072
if '(' in style_name: # Parametric style
6173
style = eval('pygal.style.' + style_name)
6274
else:
6375
style = getattr(pygal.style, style_name)
64-
for k, v in _options.items():
76+
for k, v in options.items():
6577
try:
6678
options[k] = literal_eval(v)
6779
except Exception:
@@ -73,9 +85,6 @@ def handler(self, chart_type, **_options):
7385
if _site and _site.invariant:
7486
chart.no_prefix = True
7587
chart.config(**options)
76-
for line in data.splitlines():
77-
line = line.strip()
78-
if line:
79-
label, series = literal_eval('({0})'.format(line))
80-
chart.add(label, series)
88+
for label, series in chart_data:
89+
chart.add(label, series)
8190
return chart.render().decode('utf8')

0 commit comments

Comments
 (0)
Please sign in to comment.