Unverified Commit 5607ab47 authored by Duggan, John's avatar Duggan, John Committed by GitHub
Browse files

Merge pull request #133 from nova-model/126-matplotlibfigure---add-binding-support-for-parameters

Add binding support for MatplotlibFigure.figure
parents 6e9e626d 2177f6ce
Loading
Loading
Loading
Loading
Loading
+791 −632

File changed.

Preview size limit exceeded, changes collapsed.

+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ packages = [{include = "nova", from = "src"}]
altair = "*"
libsass = "*"
mergedeep = "*"
mpld3 = "^0.5.11"
python = ">=3.10,<4.0"
tomli = "*"
tornado = ">=6.5.0"
+8 −1
Original line number Diff line number Diff line
@@ -7,6 +7,13 @@ from trame_server.core import State
from nova.mvvm._internal.utils import rgetdictvalue, rsetdictvalue


# Trame state handlers don't work on nested properties. When writing Trame state handlers (e.g. flushState, dirty, or
# change), we instead use the name of the top-level property. For example, "config.parameter_group_a.option_x" becomes
# "config".
def get_state_name(name: str) -> str:
    return name.split(".")[0]


# Reads a state parameter from Trame. For internal use only, if you're using this in your application you're violating
# the MVVM framework. :)
def get_state_param(state: State, value: Union[Any, Tuple]) -> Any:
@@ -25,6 +32,6 @@ def set_state_param(state: State, value: Union[Any, Tuple], new_value: Any = Non
                rsetdictvalue(state, value[0], new_value)
            elif len(value) > 1:
                rsetdictvalue(state, value[0], value[1])
            state.dirty(value[0].split(".")[0])
            state.dirty(get_state_name(value[0]))

    return get_state_param(state, value)
+4 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ class Interactive2DPlot(vega.Figure):

        Parameters
        ----------
        figure : `altair.Chart <https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html#altair.Chart>`_
        figure : `altair.Chart <https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html#altair.Chart>`__, optional
            Altair chart object
        kwargs
            Arguments to be passed to `AbstractElement <https://trame.readthedocs.io/en/latest/core.widget.html#trame_client.widgets.core.AbstractElement>`_
@@ -38,7 +38,7 @@ class Interactive2DPlot(vega.Figure):
        Returns
        -------
        None
        """
        """  # noqa: E501
        self._initialized = False

        super().__init__(figure=figure, **kwargs)
@@ -83,3 +83,5 @@ class Interactive2DPlot(vega.Figure):

        if hasattr(self, "_start_update_handlers"):
            self._start_update_handlers()

        self.server.state.flush()
+9 −5
Original line number Diff line number Diff line
@@ -200,18 +200,20 @@ class MatplotlibFigure(matplotlib.Figure):

        Parameters
        ----------
        figure : `altair.Chart <https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html#altair.Chart>`_
            Altair chart object
        webagg : bool
        figure : `matplotlib.figure.Figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure>`__, optional
            Initial Matplotlib figure.
        webagg : bool, optional
            If true, then the WebAgg backend for Matplotlib is used. If not, then the default Trame matplotlib plugin
            is used.
            is used. Note that this parameter does not supporting Trame bindings since the user experiences are
            fundamentally different between the two options and toggling them is not considered a good idea by the
            author of this component.
        kwargs
            Arguments to be passed to `AbstractElement <https://trame.readthedocs.io/en/latest/core.widget.html#trame_client.widgets.core.AbstractElement>`_

        Returns
        -------
        None
        """
        """  # noqa: E501
        self._webagg = webagg
        if webagg:
            self._port = MatplotlibFigure._get_free_port()
@@ -253,6 +255,8 @@ class MatplotlibFigure(matplotlib.Figure):
        else:
            super().update(figure)

        self._server.state.flush()

    def _setup_figure_websocket(self) -> None:
        thread = Thread(target=self._mpl_run_ws_server, daemon=True)
        thread.start()