Spaces:
Running
Running
File size: 11,077 Bytes
ba2f5d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
"""
Altair Plot Sphinx Extension
============================
This extension provides a means of inserting live-rendered Altair plots within
sphinx documentation. There are two directives defined: ``altair-setup`` and
``altair-plot``. ``altair-setup`` code is used to set-up various options
prior to running the plot code. For example::
.. altair-plot::
:output: none
from altair import *
import pandas as pd
data = pd.DataFrame({'a': list('CCCDDDEEE'),
'b': [2, 7, 4, 1, 2, 6, 8, 4, 7]})
.. altair-plot::
Chart(data).mark_point().encode(
x='a',
y='b'
)
In the case of the ``altair-plot`` code, the *last statement* of the code-block
should contain the chart object you wish to be rendered.
Options
-------
The directives have the following options::
.. altair-plot::
:namespace: # specify a plotting namespace that is persistent within the doc
:hide-code: # if set, then hide the code and only show the plot
:code-below: # if set, then code is below rather than above the figure
:output: [plot|repr|stdout|none]
:alt: text # Alternate text when plot cannot be rendered
:links: editor source export # specify one or more of these options
:chart-var-name: chart # name of variable in namespace containing output
Additionally, this extension introduces a global configuration
``altairplot_links``, set in your ``conf.py`` which is a dictionary
of links that will appear below plots, unless the ``:links:`` option
again overrides it. It should look something like this::
# conf.py
# ...
altairplot_links = {'editor': True, 'source': True, 'export': True}
# ...
If this configuration is not specified, all are set to True.
"""
import contextlib
import io
import os
import json
import warnings
import jinja2
from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives import flag, unchanged
from sphinx.locale import _
import altair as alt
from altair.utils.execeval import eval_block
# These default URLs can be changed in conf.py; see setup() below.
VEGA_JS_URL_DEFAULT = "https://cdn.jsdelivr.net/npm/vega@{}".format(alt.VEGA_VERSION)
VEGALITE_JS_URL_DEFAULT = "https://cdn.jsdelivr.net/npm/vega-lite@{}".format(
alt.VEGALITE_VERSION
)
VEGAEMBED_JS_URL_DEFAULT = "https://cdn.jsdelivr.net/npm/vega-embed@{}".format(
alt.VEGAEMBED_VERSION
)
VGL_TEMPLATE = jinja2.Template(
"""
<div id="{{ div_id }}">
<script>
// embed when document is loaded, to ensure vega library is available
// this works on all modern browsers, except IE8 and older
document.addEventListener("DOMContentLoaded", function(event) {
var spec = {{ spec }};
var opt = {
"mode": "{{ mode }}",
"renderer": "{{ renderer }}",
"actions": {{ actions}}
};
vegaEmbed('#{{ div_id }}', spec, opt).catch(console.err);
});
</script>
</div>
"""
)
class altair_plot(nodes.General, nodes.Element):
pass
def purge_altair_namespaces(app, env, docname):
if not hasattr(env, "_altair_namespaces"):
return
env._altair_namespaces.pop(docname, {})
DEFAULT_ALTAIRPLOT_LINKS = {"editor": True, "source": True, "export": True}
def validate_links(links):
if links.strip().lower() == "none":
return False
links = links.strip().split()
diff = set(links) - set(DEFAULT_ALTAIRPLOT_LINKS.keys())
if diff:
raise ValueError("Following links are invalid: {}".format(list(diff)))
return {link: link in links for link in DEFAULT_ALTAIRPLOT_LINKS}
def validate_output(output):
output = output.strip().lower()
if output not in ["plot", "repr", "stdout", "none"]:
raise ValueError(":output: flag must be one of [plot|repr|stdout|none]")
return output
class AltairPlotDirective(Directive):
has_content = True
option_spec = {
"hide-code": flag,
"code-below": flag,
"namespace": unchanged,
"output": validate_output,
"alt": unchanged,
"links": validate_links,
"chart-var-name": unchanged,
}
def run(self):
env = self.state.document.settings.env
app = env.app
show_code = "hide-code" not in self.options
code_below = "code-below" in self.options
if not hasattr(env, "_altair_namespaces"):
env._altair_namespaces = {}
namespace_id = self.options.get("namespace", "default")
namespace = env._altair_namespaces.setdefault(env.docname, {}).setdefault(
namespace_id, {}
)
code = "\n".join(self.content)
if show_code:
source_literal = nodes.literal_block(code, code)
source_literal["language"] = "python"
# get the name of the source file we are currently processing
rst_source = self.state_machine.document["source"]
rst_dir = os.path.dirname(rst_source)
rst_filename = os.path.basename(rst_source)
# use the source file name to construct a friendly target_id
serialno = env.new_serialno("altair-plot")
rst_base = rst_filename.replace(".", "-")
div_id = "{}-altair-plot-{}".format(rst_base, serialno)
target_id = "{}-altair-source-{}".format(rst_base, serialno)
target_node = nodes.target("", "", ids=[target_id])
# create the node in which the plot will appear;
# this will be processed by html_visit_altair_plot
plot_node = altair_plot()
plot_node["target_id"] = target_id
plot_node["div_id"] = div_id
plot_node["code"] = code
plot_node["namespace"] = namespace
plot_node["relpath"] = os.path.relpath(rst_dir, env.srcdir)
plot_node["rst_source"] = rst_source
plot_node["rst_lineno"] = self.lineno
plot_node["links"] = self.options.get(
"links", app.builder.config.altairplot_links
)
plot_node["output"] = self.options.get("output", "plot")
plot_node["chart-var-name"] = self.options.get("chart-var-name", None)
if "alt" in self.options:
plot_node["alt"] = self.options["alt"]
result = [target_node]
if code_below:
result += [plot_node]
if show_code:
result += [source_literal]
if not code_below:
result += [plot_node]
return result
def html_visit_altair_plot(self, node):
# Execute the code, saving output and namespace
namespace = node["namespace"]
try:
f = io.StringIO()
with contextlib.redirect_stdout(f):
chart = eval_block(node["code"], namespace)
stdout = f.getvalue()
except Exception as e:
warnings.warn(
"altair-plot: {}:{} Code Execution failed:"
"{}: {}".format(
node["rst_source"], node["rst_lineno"], e.__class__.__name__, str(e)
)
)
raise nodes.SkipNode
chart_name = node["chart-var-name"]
if chart_name is not None:
if chart_name not in namespace:
raise ValueError(
"chart-var-name='{}' not present in namespace" "".format(chart_name)
)
chart = namespace[chart_name]
output = node["output"]
if output == "none":
raise nodes.SkipNode
elif output == "stdout":
if not stdout:
raise nodes.SkipNode
else:
output_literal = nodes.literal_block(stdout, stdout)
output_literal["language"] = "none"
node.extend([output_literal])
elif output == "repr":
if chart is None:
raise nodes.SkipNode
else:
rep = " " + repr(chart).replace("\n", "\n ")
repr_literal = nodes.literal_block(rep, rep)
repr_literal["language"] = "none"
node.extend([repr_literal])
elif output == "plot":
if isinstance(chart, alt.TopLevelMixin):
# Last line should be a chart; convert to spec dict
try:
spec = chart.to_dict()
except alt.utils.schemapi.SchemaValidationError:
raise ValueError("Invalid chart: {0}".format(node["code"]))
actions = node["links"]
# TODO: add an option to save spects to file & load from there.
# TODO: add renderer option
# Write spec to a *.vl.json file
# dest_dir = os.path.join(self.builder.outdir, node['relpath'])
# if not os.path.exists(dest_dir):
# os.makedirs(dest_dir)
# filename = "{0}.vl.json".format(node['target_id'])
# dest_path = os.path.join(dest_dir, filename)
# with open(dest_path, 'w') as f:
# json.dump(spec, f)
# Pass relevant info into the template and append to the output
html = VGL_TEMPLATE.render(
div_id=node["div_id"],
spec=json.dumps(spec),
mode="vega-lite",
renderer="canvas",
actions=json.dumps(actions),
)
self.body.append(html)
else:
warnings.warn(
"altair-plot: {}:{} Malformed block. Last line of "
"code block should define a valid altair Chart object."
"".format(node["rst_source"], node["rst_lineno"])
)
raise nodes.SkipNode
def generic_visit_altair_plot(self, node):
# TODO: generate PNGs and insert them here
if "alt" in node.attributes:
self.body.append(_("[ graph: %s ]") % node["alt"])
else:
self.body.append(_("[ graph ]"))
raise nodes.SkipNode
def depart_altair_plot(self, node):
return
def builder_inited(app):
app.add_js_file(app.config.altairplot_vega_js_url)
app.add_js_file(app.config.altairplot_vegalite_js_url)
app.add_js_file(app.config.altairplot_vegaembed_js_url)
def setup(app):
setup.app = app
setup.config = app.config
setup.confdir = app.confdir
app.add_config_value("altairplot_links", DEFAULT_ALTAIRPLOT_LINKS, "env")
app.add_config_value("altairplot_vega_js_url", VEGA_JS_URL_DEFAULT, "html")
app.add_config_value("altairplot_vegalite_js_url", VEGALITE_JS_URL_DEFAULT, "html")
app.add_config_value(
"altairplot_vegaembed_js_url", VEGAEMBED_JS_URL_DEFAULT, "html"
)
app.add_directive("altair-plot", AltairPlotDirective)
app.add_css_file("altair-plot.css")
app.add_node(
altair_plot,
html=(html_visit_altair_plot, depart_altair_plot),
latex=(generic_visit_altair_plot, depart_altair_plot),
texinfo=(generic_visit_altair_plot, depart_altair_plot),
text=(generic_visit_altair_plot, depart_altair_plot),
man=(generic_visit_altair_plot, depart_altair_plot),
)
app.connect("env-purge-doc", purge_altair_namespaces)
app.connect("builder-inited", builder_inited)
return {"version": "0.1"}
|