Skip to content

Commit

Permalink
Test lazyload (#47)
Browse files Browse the repository at this point in the history
* test that lazily loaded modules are not imported by default

* pandas compability

* lazy load figure factory
  • Loading branch information
Aggrathon authored Sep 16, 2023
1 parent 08f85ff commit c4bb736
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
24 changes: 24 additions & 0 deletions tests/test_lazyload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import multiprocessing
import sys


def check_loaded(queue, modules):
import xiplot # noqa: F401
import xiplot.setup # noqa: F401

queue.put([mod in sys.modules for mod in modules])


def test_ensure_lazyload():
# This tests that the following packages are not imported when xiplot is
# imported, so that they can be lazily loaded in the WASM version.
# This test starts a new Python (multi-)process using "spawn" to check the
# imports in a clean environment.
modules = ["sklearn", "jsonschema", "plotly.figure_factory"]
ctx = multiprocessing.get_context("spawn")
q = ctx.Queue()
p = ctx.Process(target=check_loaded, args=(q, modules))
p.start()
for mod, loaded in zip(modules, q.get()):
assert not loaded, f"{mod} should be lazily loaded"
p.join()
3 changes: 2 additions & 1 deletion xiplot/plots/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def render(
):
if y_axis is None:
return placeholder_figure("Please select y axis")
df = merge_df_aux(df, aux).reset_index(names="__Xiplot_index__")
df = merge_df_aux(df, aux)
df["__Xiplot_index__"] = range(df.shape[0])
if x_axis not in df.columns:
return placeholder_figure("Please select x axis")
if color not in df.columns:
Expand Down
11 changes: 8 additions & 3 deletions xiplot/plots/distplot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dash
import plotly.express as px
import plotly.figure_factory as ff
from dash import ALL, MATCH, Input, Output, State, dcc
from dash.exceptions import PreventUpdate

Expand Down Expand Up @@ -31,7 +30,7 @@
class Distplot(APlot):
@classmethod
def name(cls):
return "Distribution plot"
return "Density plot"

@classmethod
def register_callbacks(cls, app, df_from_store, df_to_store):
Expand Down Expand Up @@ -121,7 +120,13 @@ def render(
hover=None,
template=None,
):
df = merge_df_aux(df, aux).reset_index(names="__Xiplot_index__")
# `plotly.figure_factory` checks if scipy is installed and caches the
# result. This causes issues if scipy is lazily loaded in WASM.
import scipy # noqa: F401, isort: skip
import plotly.figure_factory as ff

df = merge_df_aux(df, aux)
df["__Xiplot_index__"] = range(df.shape[0])
if variable not in df.columns:
return placeholder_figure("Please select a variable")
if color not in df.columns:
Expand Down
3 changes: 2 additions & 1 deletion xiplot/plots/lineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def render(
hover=None,
template=None,
):
df = merge_df_aux(df, aux).reset_index(names="__Xiplot_index__")
df = merge_df_aux(df, aux)
df["__Xiplot_index__"] = range(df.shape[0])
if x_axis not in df.columns or y_axis not in df.columns:
return placeholder_figure("Please select x and y axis")
if color not in df.columns:
Expand Down

0 comments on commit c4bb736

Please sign in to comment.