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 dataclasses
from typing import Optional

import leda
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
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: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:00<00:00, 60.23it/s]
Generating HTML: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:00<00:00, 136.69it/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: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:00<00:00, 531.26it/s]
Generating HTML: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:00<00:00, 368.45it/s]
Out[8]:
column:
window:

Objects as Params

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


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

    
@dataclasses.dataclass(frozen=True)
class EWMMeanCalculator(Calculator):
    com: float
    
    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)])
plot_df(calced_df, title=f"column_group={column_group!r}, calc={calc}")
Generating results: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 280.80it/s]
Generating HTML: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 309.79it/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]: