From 7143969d1cb5e3ce5f0c8d8ce847bf6fa38c7f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Filip?= Date: Thu, 9 Feb 2023 11:10:52 +0100 Subject: [PATCH 1/3] Add potential fix for increasing map size bug --- python/ipywidgets/ipywidgets/embed.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/python/ipywidgets/ipywidgets/embed.py b/python/ipywidgets/ipywidgets/embed.py index ca58880092..2abbde0597 100644 --- a/python/ipywidgets/ipywidgets/embed.py +++ b/python/ipywidgets/ipywidgets/embed.py @@ -12,10 +12,12 @@ import json import re -from .widgets import Widget, DOMWidget, widget as widget_module -from .widgets.widget_link import Link -from .widgets.docutils import doc_subst + from ._version import __html_manager_version__ +from .widgets import DOMWidget, Widget +from .widgets import widget as widget_module +from .widgets.docutils import doc_subst +from .widgets.widget_link import Link snippet_template = """ {load} @@ -299,7 +301,21 @@ def embed_minimal_html(fp, views, title='IPyWidget export', template=None, **kwa will be replaced by all the widgets. {embed_kwargs} """ - snippet = embed_snippet(views, **kwargs) + + if kwargs.get("state") is None: + state = dependency_state(views, drop_defaults=kwargs.get("drop_defaults")) + else: + state = kwargs["state"] + + snippet = embed_snippet( + views, + drop_defaults=kwargs.get("drop_defaults", True), + state=state, + indent=kwargs.get("indent", 2), + embed_url=kwargs.get("embed_url", None), + requirejs=kwargs.get("requirejs", True), + cors=kwargs.get("cors", True) + ) values = { 'title': title, From b8a89ab6d7233088d7d873e42e5e3e16c3baa3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Filip?= Date: Thu, 9 Feb 2023 13:30:16 +0100 Subject: [PATCH 2/3] Make arguments explicit and add documentation --- python/ipywidgets/ipywidgets/embed.py | 52 ++++++++++++++++++++------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/python/ipywidgets/ipywidgets/embed.py b/python/ipywidgets/ipywidgets/embed.py index 2abbde0597..268ef51238 100644 --- a/python/ipywidgets/ipywidgets/embed.py +++ b/python/ipywidgets/ipywidgets/embed.py @@ -286,7 +286,18 @@ def embed_snippet(views, @doc_subst(_doc_snippets) -def embed_minimal_html(fp, views, title='IPyWidget export', template=None, **kwargs): +def embed_minimal_html( + fp, + views, + title='IPyWidget export', + template=None, + drop_defaults=True, + state=None, + indent=2, + embed_url=None, + requirejs=True, + cors=True, +): """Write a minimal HTML file with widget views embedded. Parameters @@ -299,22 +310,39 @@ def embed_minimal_html(fp, views, title='IPyWidget export', template=None, **kwa This should be a Python string with placeholders `{{title}}` and `{{snippet}}`. The `{{snippet}}` placeholder will be replaced by all the widgets. - {embed_kwargs} + drop_defaults: boolean + Whether to drop default values from the widget states. + state: dict or None (default) + The state to include. When set to None, the state of all passed views + is included. Otherwise it uses the passed state directly. This allows + for end users to include a smaller state, under the responsibility that + this state is sufficient to reconstruct the embedded views. + indent: integer, string or None + The indent to use for the JSON state dump. See `json.dumps` for + full description. + embed_url: string or None + Allows for overriding the URL used to fetch the widget manager + for the embedded code. This defaults (None) to a `jsDelivr` CDN url. + requirejs: boolean (True) + Enables the requirejs-based embedding, which allows for custom widgets. + If True, the embed_url should point to an AMD module. + cors: boolean (True) + If True avoids sending user credentials while requesting the scripts. + When opening an HTML file from disk, some browsers may refuse to load + the scripts. """ - if kwargs.get("state") is None: - state = dependency_state(views, drop_defaults=kwargs.get("drop_defaults")) - else: - state = kwargs["state"] - + if state is None: + state = dependency_state(views, drop_defaults=drop_defaults) + snippet = embed_snippet( views, - drop_defaults=kwargs.get("drop_defaults", True), + drop_defaults=drop_defaults, state=state, - indent=kwargs.get("indent", 2), - embed_url=kwargs.get("embed_url", None), - requirejs=kwargs.get("requirejs", True), - cors=kwargs.get("cors", True) + indent=indent, + embed_url=embed_url, + requirejs=requirejs, + cors=cors ) values = { From 10aae64a411af4847efa137d6a31f4f4d999a2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Filip?= Date: Tue, 14 Feb 2023 11:43:56 +0100 Subject: [PATCH 3/3] Add mode for manager state --- python/ipywidgets/ipywidgets/embed.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/python/ipywidgets/ipywidgets/embed.py b/python/ipywidgets/ipywidgets/embed.py index 268ef51238..8c270d0957 100644 --- a/python/ipywidgets/ipywidgets/embed.py +++ b/python/ipywidgets/ipywidgets/embed.py @@ -312,11 +312,13 @@ def embed_minimal_html( will be replaced by all the widgets. drop_defaults: boolean Whether to drop default values from the widget states. - state: dict or None (default) - The state to include. When set to None, the state of all passed views - is included. Otherwise it uses the passed state directly. This allows - for end users to include a smaller state, under the responsibility that - this state is sufficient to reconstruct the embedded views. + state: dict, string or None (default) + The state to include. When set to None or "dependent", the state of + all passed views is included. When set to "complete", the complete + state of the widget is included. Otherwise it uses the passed state + directly. This allows for end users to include a smaller state, under + the responsibility that this state is sufficient to reconstruct the + embedded views. indent: integer, string or None The indent to use for the JSON state dump. See `json.dumps` for full description. @@ -332,8 +334,10 @@ def embed_minimal_html( the scripts. """ - if state is None: + if state is None or state == "dependent": state = dependency_state(views, drop_defaults=drop_defaults) + elif state == "complete": + state = Widget.get_manager_state(drop_defaults=drop_defaults, widgets=None)['state'] snippet = embed_snippet( views,