Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type hinting for plotly #4279

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 78 additions & 77 deletions reflex/components/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from __future__ import annotations

from typing import Any, Dict, List
from typing import Any, Dict, List, Tuple, Union

from typing_extensions import TypedDict, TypeVar

from reflex.base import Base
from reflex.components.component import Component, NoSSRComponent
from reflex.components.core.cond import color_mode_cond
from reflex.event import EventHandler
from reflex.event import EventHandler, no_args_event_spec
from reflex.utils import console
from reflex.vars.base import LiteralVar, Var

Expand All @@ -21,19 +22,7 @@
Template = Any # type: ignore


def _event_data_signature(e0: Var) -> List[Any]:
"""For plotly events with event data and no points.

Args:
e0: The event data.

Returns:
The event key extracted from the event data (if defined).
"""
return [Var(_js_expr=f"{e0}?.event")]


def _event_points_data_signature(e0: Var) -> List[Any]:
def _event_points_data_signature(e0: Var) -> Tuple[Var[List[Point]]]:
"""For plotly events with event data containing a point array.

Args:
Expand All @@ -42,51 +31,63 @@ def _event_points_data_signature(e0: Var) -> List[Any]:
Returns:
The event data and the extracted points.
"""
return [
Var(_js_expr=f"{e0}?.event"),
Var(_js_expr=f"extractPoints({e0}?.points)"),
return (Var(_js_expr=f"extractPoints({e0}?.points)"),)


T = TypeVar("T")

ItemOrList = Union[T, List[T]]


class BBox(TypedDict):
"""Bounding box for a point in a plotly graph."""

x0: Union[float, int, None]
x1: Union[float, int, None]
y0: Union[float, int, None]
y1: Union[float, int, None]
z0: Union[float, int, None]
z1: Union[float, int, None]


class Point(TypedDict):
"""A point in a plotly graph."""

x: Union[float, int, None]
y: Union[float, int, None]
z: Union[float, int, None]
lat: Union[float, int, None]
lon: Union[float, int, None]
curveNumber: Union[int, None]
pointNumber: Union[int, None]
pointNumbers: Union[List[int], None]
pointIndex: Union[int, None]
markerColor: Union[
ItemOrList[
ItemOrList[
Union[
float,
int,
str,
None,
]
]
],
None,
]


class _ButtonClickData(Base):
"""Event data structure for plotly UI buttons."""

menu: Any
button: Any
active: Any


def _button_click_signature(e0: _ButtonClickData) -> List[Any]:
"""For plotly button click events.

Args:
e0: The button click data.

Returns:
The menu, button, and active state.
"""
return [e0.menu, e0.button, e0.active]


def _passthrough_signature(e0: Var) -> List[Any]:
"""For plotly events with arbitrary serializable data, passed through directly.

Args:
e0: The event data.

Returns:
The event data.
"""
return [e0]


def _null_signature() -> List[Any]:
"""For plotly events with no data or non-serializable data. Nothing passed through.

Returns:
An empty list (nothing passed through).
"""
return []
markerSize: Union[
ItemOrList[
ItemOrList[
Union[
float,
int,
None,
]
]
],
None,
]
bbox: Union[BBox, None]


class Plotly(NoSSRComponent):
Expand Down Expand Up @@ -116,49 +117,49 @@ class Plotly(NoSSRComponent):
use_resize_handler: Var[bool] = LiteralVar.create(True)

# Fired after the plot is redrawn.
on_after_plot: EventHandler[_passthrough_signature]
on_after_plot: EventHandler[no_args_event_spec]

# Fired after the plot was animated.
on_animated: EventHandler[_null_signature]
on_animated: EventHandler[no_args_event_spec]

# Fired while animating a single frame (does not currently pass data through).
on_animating_frame: EventHandler[_null_signature]
on_animating_frame: EventHandler[no_args_event_spec]

# Fired when an animation is interrupted (to start a new animation for example).
on_animation_interrupted: EventHandler[_null_signature]
on_animation_interrupted: EventHandler[no_args_event_spec]

# Fired when the plot is responsively sized.
on_autosize: EventHandler[_event_data_signature]
on_autosize: EventHandler[no_args_event_spec]

# Fired whenever mouse moves over a plot.
on_before_hover: EventHandler[_event_data_signature]
on_before_hover: EventHandler[no_args_event_spec]

# Fired when a plotly UI button is clicked.
on_button_clicked: EventHandler[_button_click_signature]
on_button_clicked: EventHandler[no_args_event_spec]

# Fired when the plot is clicked.
on_click: EventHandler[_event_points_data_signature]

# Fired when a selection is cleared (via double click).
on_deselect: EventHandler[_null_signature]
on_deselect: EventHandler[no_args_event_spec]

# Fired when the plot is double clicked.
on_double_click: EventHandler[_passthrough_signature]
on_double_click: EventHandler[no_args_event_spec]

# Fired when a plot element is hovered over.
on_hover: EventHandler[_event_points_data_signature]

# Fired after the plot is layed out (zoom, pan, etc).
on_relayout: EventHandler[_passthrough_signature]
on_relayout: EventHandler[no_args_event_spec]

# Fired while the plot is being layed out.
on_relayouting: EventHandler[_passthrough_signature]
on_relayouting: EventHandler[no_args_event_spec]

# Fired after the plot style is changed.
on_restyle: EventHandler[_passthrough_signature]
on_restyle: EventHandler[no_args_event_spec]

# Fired after the plot is redrawn.
on_redraw: EventHandler[_event_data_signature]
on_redraw: EventHandler[no_args_event_spec]

# Fired after selecting plot elements.
on_selected: EventHandler[_event_points_data_signature]
Expand All @@ -167,10 +168,10 @@ class Plotly(NoSSRComponent):
on_selecting: EventHandler[_event_points_data_signature]

# Fired while an animation is occuring.
on_transitioning: EventHandler[_event_data_signature]
on_transitioning: EventHandler[no_args_event_spec]

# Fired when a transition is stopped early.
on_transition_interrupted: EventHandler[_event_data_signature]
on_transition_interrupted: EventHandler[no_args_event_spec]

# Fired when a hovered element is no longer hovered.
on_unhover: EventHandler[_event_points_data_signature]
Expand Down Expand Up @@ -216,8 +217,8 @@ def add_custom_code(self) -> list[str]:
pointNumber: point.pointNumber,
pointNumbers: point.pointNumbers,
pointIndex: point.pointIndex,
'marker.color': point['marker.color'],
'marker.size': point['marker.size'],
markerColor: point['marker.color'],
markerSize: point['marker.size'],
bbox: bbox,
})
})
Expand Down
72 changes: 46 additions & 26 deletions reflex/components/plotly/plotly.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# ------------------- DO NOT EDIT ----------------------
# This file was generated by `reflex/utils/pyi_generator.py`!
# ------------------------------------------------------
from typing import Any, Dict, Optional, Union, overload
from typing import Any, Dict, List, Optional, Union, overload

from typing_extensions import TypedDict, TypeVar

from reflex.base import Base
from reflex.components.component import NoSSRComponent
from reflex.event import BASE_STATE, EventType
from reflex.style import Style
Expand All @@ -20,11 +21,30 @@ except ImportError:
console.warn("Plotly is not installed. Please run `pip install plotly`.")
Figure = Any
Template = Any
T = TypeVar("T")
ItemOrList = Union[T, List[T]]

class BBox(TypedDict):
x0: Union[float, int, None]
x1: Union[float, int, None]
y0: Union[float, int, None]
y1: Union[float, int, None]
z0: Union[float, int, None]
z1: Union[float, int, None]

class _ButtonClickData(Base):
menu: Any
button: Any
active: Any
class Point(TypedDict):
x: Union[float, int, None]
y: Union[float, int, None]
z: Union[float, int, None]
lat: Union[float, int, None]
lon: Union[float, int, None]
curveNumber: Union[int, None]
pointNumber: Union[int, None]
pointNumbers: Union[List[int], None]
pointIndex: Union[int, None]
markerColor: Union[ItemOrList[ItemOrList[Union[float, int, str, None]]], None]
markerSize: Union[ItemOrList[ItemOrList[Union[float, int, None]]], None]
bbox: Union[BBox, None]

class Plotly(NoSSRComponent):
def add_imports(self) -> dict[str, str]: ...
Expand All @@ -45,20 +65,20 @@ class Plotly(NoSSRComponent):
class_name: Optional[Any] = None,
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_after_plot: Optional[EventType[..., BASE_STATE]] = None,
on_animated: Optional[EventType[..., BASE_STATE]] = None,
on_animating_frame: Optional[EventType[..., BASE_STATE]] = None,
on_animation_interrupted: Optional[EventType[..., BASE_STATE]] = None,
on_autosize: Optional[EventType[..., BASE_STATE]] = None,
on_before_hover: Optional[EventType[..., BASE_STATE]] = None,
on_after_plot: Optional[EventType[[], BASE_STATE]] = None,
on_animated: Optional[EventType[[], BASE_STATE]] = None,
on_animating_frame: Optional[EventType[[], BASE_STATE]] = None,
on_animation_interrupted: Optional[EventType[[], BASE_STATE]] = None,
on_autosize: Optional[EventType[[], BASE_STATE]] = None,
on_before_hover: Optional[EventType[[], BASE_STATE]] = None,
on_blur: Optional[EventType[[], BASE_STATE]] = None,
on_button_clicked: Optional[EventType[..., BASE_STATE]] = None,
on_click: Optional[EventType[..., BASE_STATE]] = None,
on_button_clicked: Optional[EventType[[], BASE_STATE]] = None,
on_click: Optional[EventType[[List[Point]], BASE_STATE]] = None,
on_context_menu: Optional[EventType[[], BASE_STATE]] = None,
on_deselect: Optional[EventType[..., BASE_STATE]] = None,
on_double_click: Optional[EventType[..., BASE_STATE]] = None,
on_deselect: Optional[EventType[[], BASE_STATE]] = None,
on_double_click: Optional[EventType[[], BASE_STATE]] = None,
on_focus: Optional[EventType[[], BASE_STATE]] = None,
on_hover: Optional[EventType[..., BASE_STATE]] = None,
on_hover: Optional[EventType[[List[Point]], BASE_STATE]] = None,
on_mount: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_down: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_enter: Optional[EventType[[], BASE_STATE]] = None,
Expand All @@ -67,16 +87,16 @@ class Plotly(NoSSRComponent):
on_mouse_out: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_over: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_up: Optional[EventType[[], BASE_STATE]] = None,
on_redraw: Optional[EventType[..., BASE_STATE]] = None,
on_relayout: Optional[EventType[..., BASE_STATE]] = None,
on_relayouting: Optional[EventType[..., BASE_STATE]] = None,
on_restyle: Optional[EventType[..., BASE_STATE]] = None,
on_redraw: Optional[EventType[[], BASE_STATE]] = None,
on_relayout: Optional[EventType[[], BASE_STATE]] = None,
on_relayouting: Optional[EventType[[], BASE_STATE]] = None,
on_restyle: Optional[EventType[[], BASE_STATE]] = None,
on_scroll: Optional[EventType[[], BASE_STATE]] = None,
on_selected: Optional[EventType[..., BASE_STATE]] = None,
on_selecting: Optional[EventType[..., BASE_STATE]] = None,
on_transition_interrupted: Optional[EventType[..., BASE_STATE]] = None,
on_transitioning: Optional[EventType[..., BASE_STATE]] = None,
on_unhover: Optional[EventType[..., BASE_STATE]] = None,
on_selected: Optional[EventType[[List[Point]], BASE_STATE]] = None,
on_selecting: Optional[EventType[[List[Point]], BASE_STATE]] = None,
on_transition_interrupted: Optional[EventType[[], BASE_STATE]] = None,
on_transitioning: Optional[EventType[[], BASE_STATE]] = None,
on_unhover: Optional[EventType[[List[Point]], BASE_STATE]] = None,
on_unmount: Optional[EventType[[], BASE_STATE]] = None,
**props,
) -> "Plotly":
Expand Down
Loading