Skip to content

Commit 9e73893

Browse files
committed
fix(plot.jupy) parsing defaults fix on strings >
while it's possible to - refact: PanZoom args now as dict (not string) - doc: improve advice for sizing jupyter plots, to reuse & override existing defaults.
1 parent 8ea0adb commit 9e73893

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

docs/source/plotting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Configurations
378378
.. confval:: graphtik_zoomable_options
379379

380380
- type: `str`
381-
- default: ``{controlIconsEnabled: true, fit: true}``
381+
- default: ``{"controlIconsEnabled": true, "fit": true}``
382382

383383
A JS-object with `the options <https://github.com/bumbu/svg-pan-zoom#how-to-use>`_
384384
for the interactive zoom+pan pf SVGs, when the ``:zoomable-opts:`` directive option

graphtik/plot.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,21 @@ def is_empty_frame(val):
8383
pass
8484

8585
#: A nested dictionary controlling the rendering of graph-plots in Jupyter cells,
86-
#:
87-
#: as those returned from :meth:`.Plottable.plot()` (currently as SVGs).
86+
#:#: as those returned from :meth:`.Plottable.plot()` (currently as SVGs).
8887
#: Either modify it in place, or pass another one in the respective methods.
8988
#:
89+
#: Any keys ommitted, are taken from :data:`default_jupyter_render` - pass
90+
#: an empty dict(``{}``) if that is not desirable
91+
#: (eg. to revert to panZoom's library's defaults).
92+
#:
9093
#: The following keys are supported.
9194
#:
9295
#: :param svg_pan_zoom_json:
9396
#: arguments controlling the rendering of a zoomable SVG in
9497
#: Jupyter notebooks, as defined in https://github.com/bumbu/svg-pan-zoom#how-to-use
95-
#: if `None`, defaults to string (also maps supported)::
98+
#: if `None`, defaults to this map (json strings also supported)::
9699
#:
97-
#: "{controlIconsEnabled: true, fit: true}"
100+
#: {"controlIconsEnabled": True, "fit": True}"
98101
#:
99102
#: :param svg_element_styles:
100103
#: mostly for sizing the zoomable SVG in Jupyter notebooks.
@@ -107,10 +110,10 @@ def is_empty_frame(val):
107110
#: like `svg_element_styles`, if `None`, defaults to empty string (also maps supported).
108111
#:
109112
#: .. note::
110-
#: referred also by :rst:dir:`graphtik`'s :confval:`graphtik_zoomable_options`
113+
#: referred also by :rst:dir:`graphtik`'s :confval:`graphtik_zoomable_options`
111114
#: default configuration value.
112115
default_jupyter_render = {
113-
"svg_pan_zoom_json": "{controlIconsEnabled: true, fit: true}",
116+
"svg_pan_zoom_json": {"controlIconsEnabled": True, "fit": True},
114117
"svg_element_styles": "width: 100%; height: 300px;",
115118
"svg_container_styles": "",
116119
}
@@ -122,18 +125,20 @@ def _parse_jupyter_render(dot) -> Tuple[str, str, str]:
122125
jupy_cfg = default_jupyter_render
123126

124127
def parse_value(key: str, parser: Callable) -> str:
125-
if key not in jupy_cfg:
126-
return parser(default_jupyter_render.get(key, ""))
128+
val: Union[Mapping, str] = jupy_cfg.get(key, default_jupyter_render.get(key))
127129

128-
val: Union[Mapping, str] = jupy_cfg.get(key)
129130
if not val:
130131
val = ""
131132
elif not isinstance(val, str):
132133
val = parser(val)
133134
return val
134135

135136
def styles_parser(d: Mapping) -> str:
136-
return "".join(f"{key}: {val};\n" for key, val in d)
137+
try:
138+
return "".join(f"{key}: {val};\n" for key, val in d)
139+
except Exception as ex:
140+
breakpoint()
141+
raise ValueError(f"Failed to parse styles {d!r} due to: {ex}") from ex
137142

138143
svg_container_styles = parse_value("svg_container_styles", styles_parser)
139144
svg_element_styles = parse_value("svg_element_styles", styles_parser)
@@ -1817,13 +1822,21 @@ def render_pydot(self, dot: pydot.Dot, filename=None, jupyter_render: str = None
18171822
:seealso: :attr:`.PlotArgs.filename`
18181823
:param jupyter_render:
18191824
a nested dictionary controlling the rendering of graph-plots in Jupyter cells.
1820-
If `None`, defaults to :data:`default_jupyter_render`; you may modify those
1825+
If `None`, defaults to :data:`default_jupyter_render`; values for
1826+
ommitted sub-keys are taken also from the above dict - pass an empty dict(``{}``)
1827+
if that is not desirable (eg. to revert to panZoom's library's defaults).
1828+
1829+
You may modify those
18211830
in place and they will apply for all future calls (see :ref:`jupyter_rendering`).
18221831
18231832
You may increase the height of the SVG cell output with
18241833
something like this::
18251834
1826-
plottable.plot(jupyter_render={"svg_element_styles": "height: 600px; width: 100%"})
1835+
from graphtik.plot import default_jupyter_render
1836+
plottable.plot(jupyter_render={
1837+
**default_jupyter_render,
1838+
"svg_element_styles": "height: 600px; width: 100%"
1839+
})
18271840
:return:
18281841
the matplotlib image if ``filename=-1``, or the given `dot` annotated with any
18291842
jupyter-rendering configurations given in `jupyter_render` parameter.

0 commit comments

Comments
 (0)