Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict option to sphinx extension #2551

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion altair/sphinxext/altairgallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

.. altair-plot::
{% if code_below %}:code-below:{% endif %}
{% if strict %}:strict:{% endif %}

{{ code | indent(4) }}

Expand Down Expand Up @@ -253,7 +254,7 @@ def main(app):

gallery_ref = app.builder.config.altair_gallery_ref
gallery_title = app.builder.config.altair_gallery_title
examples = populate_examples(gallery_ref=gallery_ref, code_below=True)
examples = populate_examples(gallery_ref=gallery_ref, code_below=True, strict=False)

if not os.path.exists(target_dir):
os.makedirs(target_dir)
Expand Down
17 changes: 11 additions & 6 deletions altair/sphinxext/altairplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
: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
:strict: # if set, then code with errors will raise instead of being skipped


Additionally, this extension introduces a global configuration
Expand Down Expand Up @@ -142,6 +143,7 @@ class AltairPlotDirective(Directive):
"alt": unchanged,
"links": validate_links,
"chart-var-name": unchanged,
"strict": flag,
}

def run(self):
Expand All @@ -150,6 +152,7 @@ def run(self):

show_code = "hide-code" not in self.options
code_below = "code-below" in self.options
strict = "strict" in self.options

if not hasattr(env, "_altair_namespaces"):
env._altair_namespaces = {}
Expand Down Expand Up @@ -191,6 +194,7 @@ def run(self):
)
plot_node["output"] = self.options.get("output", "plot")
plot_node["chart-var-name"] = self.options.get("chart-var-name", None)
plot_node["strict"] = strict

if "alt" in self.options:
plot_node["alt"] = self.options["alt"]
Expand All @@ -216,13 +220,14 @@ def html_visit_altair_plot(self, node):
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)
)
message = "altair-plot: {}:{} Code Execution failed:" "{}: {}".format(
node["rst_source"], node["rst_lineno"], e.__class__.__name__, str(e)
)
raise nodes.SkipNode
if node["strict"]:
raise ValueError(message) from e
else:
warnings.warn(message)
raise nodes.SkipNode

chart_name = node["chart-var-name"]
if chart_name is not None:
Expand Down