In [1]:
# Auto-generated by leda
import leda


leda.set_interact_mode(leda.StaticIpywidgetsInteractMode())
In [2]:
# Auto-generated by leda
import os

from leda.vendor.static_ipywidgets.static_ipywidgets     import interact as static_interact


static_interact.IMAGE_MANAGER = static_interact.InlineImageManager()

leda demo: plotly

In [3]:
import abc
import dataclasses
from typing import Optional

import numpy as np
import pandas as pd
import plotly.graph_objs as go
from typing_extensions import override

import leda
In [4]:
leda.init("plotly")

Info

Widgets

Use the %%interact expr0;expr1;... cell magic to set widgets for that cell.

Each expression is of the form x=y, where x becomes the local var of the cell and y can be a:

  • list to indicate choices for a dropdown widget
  • tuple to indicate values for an int slider (start, stop, and optional step).

E.g.:

%%interact column=list(df.columns)
%%interact column=list(df.columns);mult=[1, 2, 3]
%%interact column=list(df.columns);window=(10, 50)
%%interact column=list(df.columns);window=(10, 50, 5)

Table of Contents

Use the %toc line magic to substitute with a table of contents in static mode.

Toggles

Click the Toggle input cells button at the bottom to reveal input cells.

Data

Using randomly generated data (with fixed seed).

In [5]:
df = pd.DataFrame(
    np.random.RandomState(42).rand(100, 10), columns=list("abcdefghij")
)

Visualization

In [6]:
def plot_df(df: pd.DataFrame, title: Optional[str] = None) -> go.Figure:
    fig = go.Figure()

    colors = [
        "rgb(0, 0, 255)",
        "rgb(255, 0, 0)",
        "rgb(0, 255, 0)",
        "rgb(0, 0, 0)",
        "rgb(128, 128, 128)",
        "rgb(255, 255, 0)",
        "rgb(0, 255, 255)",
        "rgb(255, 0, 255)",
        "rgb(200, 200, 200)",
    ]
    for idx, column in enumerate(df.columns):
        color = colors[idx % len(colors)]

        fig.add_trace(
            go.Scatter(
                x=df.index,
                y=df[column],
                mode="lines",
                marker={"color": color},
                name=str(column),
            )
        )

    if title:
        fig.update_layout(title=title)

    return fig

Simple

In [7]:
%%interact column=list(df.columns);mult=[1, 2, 3]
plot_df(df[[column]] * mult, title=f"column={column!r}, mult={mult}")
Generating results:   0%|          | 0/30 [00:00<?, ?it/s]
Generating results:   3%|▎         | 1/30 [00:00<00:26,  1.09it/s]
Generating results:  63%|██████▎   | 19/30 [00:01<00:00, 25.16it/s]
Generating results: 100%|██████████| 30/30 [00:01<00:00, 27.78it/s]

Generating HTML:   0%|          | 0/30 [00:00<?, ?it/s]
Generating HTML:   3%|▎         | 1/30 [00:00<00:06,  4.33it/s]
Generating HTML:  33%|███▎      | 10/30 [00:00<00:00, 36.47it/s]
Generating HTML:  63%|██████▎   | 19/30 [00:00<00:00, 55.09it/s]
Generating HTML:  93%|█████████▎| 28/30 [00:00<00:00, 66.57it/s]
Generating HTML: 100%|██████████| 30/30 [00:00<00:00, 53.58it/s]

Out[7]:
column:
mult:
In [8]:
%%interact column=list(df.columns);window=(10, 50, 5)
fig = plot_df(
    df[[column]].iloc[-window:], title=f"column={column!r}, window={window}"
)
fig
Generating results:   0%|          | 0/90 [00:00<?, ?it/s]
Generating results:  21%|██        | 19/90 [00:00<00:00, 182.79it/s]
Generating results:  42%|████▏     | 38/90 [00:00<00:00, 183.29it/s]
Generating results:  63%|██████▎   | 57/90 [00:00<00:00, 185.48it/s]
Generating results:  86%|████████▌ | 77/90 [00:00<00:00, 187.84it/s]
Generating results: 100%|██████████| 90/90 [00:00<00:00, 185.49it/s]

Generating HTML:   0%|          | 0/90 [00:00<?, ?it/s]
Generating HTML:  10%|█         | 9/90 [00:00<00:00, 89.62it/s]
Generating HTML:  21%|██        | 19/90 [00:00<00:00, 92.00it/s]
Generating HTML:  32%|███▏      | 29/90 [00:00<00:00, 92.62it/s]
Generating HTML:  43%|████▎     | 39/90 [00:00<00:00, 92.43it/s]
Generating HTML:  54%|█████▍    | 49/90 [00:00<00:00, 93.37it/s]
Generating HTML:  66%|██████▌   | 59/90 [00:00<00:00, 94.08it/s]
Generating HTML:  77%|███████▋  | 69/90 [00:00<00:00, 92.97it/s]
Generating HTML:  88%|████████▊ | 79/90 [00:00<00:00, 92.67it/s]
Generating HTML:  99%|█████████▉| 89/90 [00:00<00:00, 93.12it/s]
Generating HTML: 100%|██████████| 90/90 [00:00<00:00, 92.37it/s]

Out[8]:
column:
window:

Objects as Params

In [9]:
@dataclasses.dataclass(frozen=True)
class Calculator(abc.ABC):
    @abc.abstractmethod
    def calc(self, df: pd.DataFrame) -> pd.DataFrame: ...


@dataclasses.dataclass(frozen=True)
class CumSumCalculator(Calculator):
    @override
    def calc(self, df: pd.DataFrame) -> pd.DataFrame:
        return df.cumsum()


@dataclasses.dataclass(frozen=True)
class EWMMeanCalculator(Calculator):
    com: float

    @override
    def calc(self, df: pd.DataFrame) -> pd.DataFrame:
        return df.ewm(com=self.com).mean()


calcs = [
    CumSumCalculator(),
    EWMMeanCalculator(com=5),
    EWMMeanCalculator(com=10),
]
In [10]:
%%interact column_group=["abc", "def", "ghij"];calc=calcs
calced_df = calc.calc(df[list(column_group)])
title = f"column_group={column_group!r}, calc={calc}"
plot_df(calced_df, title=title)
Generating results:   0%|          | 0/9 [00:00<?, ?it/s]
Generating results: 100%|██████████| 9/9 [00:00<00:00, 107.38it/s]

Generating HTML:   0%|          | 0/9 [00:00<?, ?it/s]
Generating HTML: 100%|██████████| 9/9 [00:00<00:00, 83.83it/s]
Generating HTML: 100%|██████████| 9/9 [00:00<00:00, 82.40it/s]

Out[10]:
calc:
column_group:

In [11]:
# Auto-generated by leda
import leda


leda.show_input_toggle()
Out[11]:
In [12]:
# Auto-generated by leda
import leda


leda.show_std_output_toggle()
Out[12]: