Skip to content

Commit 8ba6845

Browse files
committed
Enhance plotter backend selection and documentation
- Implement auto-detection of the best available plotting backend (Plotly or Matplotlib). - Add runtime backend switching capability via `set_backend()` method. - Update README and documentation to reflect backend selection features. - Introduce tests for backend resolution and selection logic. - Include Plotly as an optional dependency in pyproject.toml.
1 parent d713d88 commit 8ba6845

File tree

9 files changed

+635
-62
lines changed

9 files changed

+635
-62
lines changed

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Simply run the cells to explore:
5353

5454
- **Single, stable user API**
5555
- `workspace` for data access and persistence
56-
- `plotter` for visualization
56+
- `plotter` for visualization (Plotly interactive or Matplotlib static)
5757
- `sigima` for scientific processing
5858

5959
- **Two execution modes, one notebook**
@@ -64,6 +64,11 @@ Simply run the cells to explore:
6464
- Analyses can be saved and reloaded using `.h5` files
6565
- Notebooks run unchanged across environments
6666

67+
- **Smart visualization backend**
68+
- Plotly (interactive) preferred when installed, Matplotlib (static) fallback
69+
- Switch backends at runtime with `plotter.set_backend("matplotlib")`
70+
- Override via `DATALAB_PLOTTER_BACKEND` environment variable
71+
6772
- **Performance-aware**
6873
- Optimized data handling when DataLab is attached
6974
- No unnecessary serialization for large datasets
@@ -80,7 +85,7 @@ Simply run the cells to explore:
8085
img = workspace.get("i042")
8186
filtered = sigima.proc.image.butterworth(img, cut_off=0.2)
8287
workspace.add("filtered_i042", filtered)
83-
plotter.plot("filtered_i042")
88+
plotter.plot("filtered_i042") # interactive Plotly figure (or static PNG)
8489
```
8590

8691
Depending on the execution context:
@@ -176,9 +181,10 @@ The kernel requires:
176181
- `sigima>=1.0` - Scientific signal and image processing
177182
- `numpy>=1.22`, `h5py>=3.0`, `matplotlib>=3.5`
178183

179-
Optional dependency (via `[cli]` extra):
184+
Optional dependencies (via extras):
180185

181-
- `jupyter-client>=7.0` - For `install`/`uninstall` CLI commands
186+
- `[plotly]``plotly>=5.0` for interactive plots (auto-used when installed)
187+
- `[cli]``jupyter-client>=7.0` for `install`/`uninstall` CLI commands
182188

183189
### With DataLab
184190

@@ -197,6 +203,38 @@ python -m datalab_kernel install
197203

198204
---
199205

206+
## Visualization Backends
207+
208+
DataLab-Kernel supports two plotting backends:
209+
210+
| Backend | Output | Install |
211+
| ---------- | -------------------- | -------------------------------- |
212+
| **Plotly** | Interactive HTML | `pip install datalab-kernel[plotly]` |
213+
| **Matplotlib** | Static PNG | Included by default |
214+
215+
When both are installed, **Plotly is used automatically** for its richer
216+
interactive experience. You can switch at any time:
217+
218+
```python
219+
# Check current backend
220+
print(plotter.backend) # "plotly" or "matplotlib"
221+
222+
# Switch at runtime
223+
plotter.set_backend("matplotlib")
224+
plotter.plot("my_signal") # static PNG
225+
226+
plotter.set_backend("plotly")
227+
plotter.plot("my_signal") # interactive figure
228+
```
229+
230+
Or set the default before starting the kernel via an environment variable:
231+
232+
```bash
233+
export DATALAB_PLOTTER_BACKEND=matplotlib
234+
```
235+
236+
---
237+
200238
## Persistence and Sharing
201239

202240
Workspace state can be saved and reloaded:

datalab_kernel/plotly_backend.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@
6666

6767
from datalab_kernel.workspace import DataObject, Workspace
6868

69-
# ---------------------------------------------------------------------------
70-
# Plotly availability check
71-
# ---------------------------------------------------------------------------
72-
try:
73-
import plotly # noqa: F401
74-
75-
PLOTLY_AVAILABLE = True
76-
except ImportError:
77-
PLOTLY_AVAILABLE = False
78-
7969

8070
# ---------------------------------------------------------------------------
8171
# Constants

0 commit comments

Comments
 (0)