Skip to content

mplchart.notebook

Notebook chart controls. Install with pip install 'mplchart[notebook]'.

This opt-in module requires ipywidgets and IPython. Core chart imports do not load it.


chart_widget

chart_widget(
    get_prices: Callable[[str], Any],
    *,
    ticker: str = 'AAPL',
    max_bars: int = 250,
    indicators: Iterable[Any] | None = None,
    **chart_options: Any,
)

Create a chart with centered ticker and visible-bar controls.

Return the widget as the last expression in a notebook cell, or pass it to display. Requires a live notebook kernel and an ipywidgets-compatible frontend. Inputs update on submission rather than every keystroke.

The loader receives only the ticker and returns a pandas or Polars prices DataFrame. Pass a bound method such as feed.get directly, or use functools.partial to bind query options such as frequency. No data-provider dependency is required by mplchart.

The current ticker's prices are retained when max_bars changes. This control limits the visible chart window, not the fetched history, so indicators can use earlier bars for warm-up. Switching tickers invokes the loader again; caching and refresh policy belong to the loader. Fetch and plotting errors appear in the output area so inputs remain usable.

Arguments:

  • get_prices (Callable[[str], Any]): Callable accepting a ticker and returning prices with mplchart's standard OHLCV layout. Use normalize=True for other supported column layouts.
  • ticker (str): Initial ticker. Defaults to "AAPL". Surrounding whitespace is stripped before loading; case is preserved.
  • max_bars (int): Positive number of visible bars. Defaults to 250.
  • indicators (Iterable[Any] | None): Complete sequence passed to Chart.plot, including renderers and panes. Defaults to candlesticks and volume. Use pandas indicators with a pandas loader and Polars expressions with a Polars loader.
  • **chart_options (Any): Additional Chart options, such as style, figsize, normalize, or yaxis_log. The title defaults to the ticker.

Returns:

  • VBox (widgets.VBox): Centered input row followed by the chart output area.

Raises:

  • (TypeError): If get_prices is not callable.
  • (ValueError): If the initial max_bars is not positive.

Examples:

from mplchart.notebook import chart_widget
chart_widget(feed.get, ticker="AAPL", max_bars=250)