def test_active(): shell = MockIPythonShell({}) manager = ViewManager(AutoplotDisplay(), shell, { "a": TestView(), "b": TestView() }, "a") assert manager.active == "a" manager = ViewManager(AutoplotDisplay(), shell, { "a": TestView(), "b": TestView() }, "b") assert manager.active == "b"
def test_invalid_view_name(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") with pytest.raises(ValueError): manager.active = "c"
def test_view_names(): shell = MockIPythonShell({}) manager = ViewManager(AutoplotDisplay(), shell, { "a": TestView(), "b": TestView() }, "a") assert sorted(manager.view_names) == ["a", "b"]
def test_set_plot_width(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.set_plot_width(Toast(Output()), 3) assert manager._changed assert a.plot_width == 3 assert b.plot_width is None
def test_set_ylabel(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.set_ylabel(Toast(Output()), "hello") assert manager._changed assert a.ylabel == "hello" assert b.ylabel is None
def test_max_series_length(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.update_max_series_length(Toast(Output()), 20) assert manager._changed assert a.max_series_length == 20 assert b.max_series_length is None
def test_defrost(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.defrost(Toast(Output())) assert manager._changed assert a.defrosted assert not b.defrosted
def test_rename_variable(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.rename_variable(Toast(Output()), "df", "ddf") assert manager._changed assert a.rename == ("df", "ddf") assert b.rename == ()
def test_change_colour(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.change_colour(Toast(Output()), "df", "red") assert manager._changed assert a.colour == ("df", "red") assert b.colour == ()
def test_show_variable(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") manager.show_variable(Toast(Output()), "df") assert manager._changed assert a.showed == "df" assert b.showed == ""
def test_set_active(): shell = MockIPythonShell({}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") assert manager.active == "a" assert manager.active_view is a manager.active = "b" assert manager.active == "b" assert manager.active_view is b
def test_redraw_filter_pandas(): df = pd.DataFrame({"a": [1, 2, 3]}) shell = MockIPythonShell({"n": 1, "df": df}) a = TestView() b = TestView() manager = ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "a") assert manager._changed manager.redraw() assert not manager._changed assert a.variables == {"df": df} assert b.variables == {}
def with_params(user_ns: Dict[str, Any] = None, toast: Toast = None): if user_ns is None: user_ns = {} if toast is None: toast = mock_toast shell = MockIPythonShell(user_ns) plotter = MockPlotter(toast) handler = PlotterModel(plotter) view_manager = ViewManager(AutoplotDisplay(), shell, {"graph": handler}, "graph") magic = _make_magic(shell, toast, view_manager) view_manager.redraw() return magic
def load_ipython_extension(shell): """Load the IPython extension. Called by `%load_ext ...`. This function initialises the widget output area, the autoplot display area defined in `AutoplotDisplay` and the `Plotter` instance. It also attaches adds the IPython event listeners defined in `CellEventHandler` and registers the magic commands defined in `AutoplotMagic`. Parameters ---------- shell: IPython.InteractiveShell The active IPython shell. """ js_output = widgets.Output() toast = Toast(js_output) plotter_model = PlotterModel(Plotter(toast)) dtaler = DTaler() view_manager = ViewManager(AutoplotDisplay(), shell, { "graph": plotter_model, "dtale": dtaler }, "graph") def _autoplot_post_run_cell(*args): if args: success = args[0].success else: # IPython 5.x didn't use to pass the result as a parameter of post_run_cell success = IPython.get_ipython().last_execution_succeeded if success: view_manager.redraw() # Unregister previous events registered with this class (eg.: when the plugin reloads) for name, funcs in shell.events.callbacks.items(): for func in funcs: if _autoplot_post_run_cell.__name__ == func.__name__: shell.events.unregister(name, func) shell.events.register("post_run_cell", _autoplot_post_run_cell) apm = _make_magic(shell, toast, view_manager) shell.register_magics(apm)
def with_params( user_ns: Dict[str, Any], reserved: Optional[Set[str]] = None, toast: Toast = None ) -> Tuple[MockIPythonShell, MockPlotter, PlotterModel]: if toast is None: toast = mock_toast # initialise mocks with parameters shell = MockIPythonShell(user_ns) plotter = MockPlotter(toast) # initialise cell event handler instance handler = PlotterModel(plotter) view_manager = ViewManager(AutoplotDisplay(), shell, {"graph": handler}, "graph") # optionally add reserved names if reserved is not None: view_manager._reserved = reserved view_manager.redraw() return shell, plotter, handler
def test_invalid_view_name_constructor(): shell = MockIPythonShell({}) a = TestView() b = TestView() with pytest.raises(ValueError): ViewManager(AutoplotDisplay(), shell, {"a": a, "b": b}, "c")