-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cc152d
commit cc1b5e0
Showing
8 changed files
with
245 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import Union, List, Optional | ||
import inspect | ||
import json | ||
import pathlib | ||
|
||
from typing_extensions import Literal | ||
|
||
from .pygwalker import PygWalker | ||
from pygwalker.data_parsers.base import FieldSpec | ||
from pygwalker.data_parsers.database_parser import Connector | ||
from pygwalker._typing import DataFrame, IAppearance, IThemeKey | ||
from pygwalker.services.format_invoke_walk_code import get_formated_spec_params_code_from_frame | ||
from pygwalker.communications.anywidget_comm import AnywidgetCommunication | ||
import marimo as mo | ||
import anywidget | ||
import traitlets | ||
|
||
|
||
class _WalkerWidget(anywidget.AnyWidget): | ||
"""WalkerWidget""" | ||
_esm = (pathlib.Path(__file__).parent.parent / "templates" / "dist" / "pygwalker-app.es.js").read_text() | ||
props = traitlets.Unicode("").tag(sync=True) | ||
|
||
|
||
def walk( | ||
dataset: Union[DataFrame, Connector, str], | ||
gid: Union[int, str] = None, | ||
*, | ||
field_specs: Optional[List[FieldSpec]] = None, | ||
theme_key: IThemeKey = 'g2', | ||
appearance: IAppearance = 'media', | ||
spec: str = "", | ||
show_cloud_tool: bool = True, | ||
kanaries_api_key: str = "", | ||
default_tab: Literal["data", "vis"] = "vis", | ||
**kwargs | ||
): | ||
"""Walk through pandas.DataFrame df with Graphic Walker | ||
Args: | ||
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe. | ||
- gid (Union[int, str], optional): GraphicWalker container div's id ('gwalker-{gid}') | ||
Kargs: | ||
- env: (Literal['Jupyter' | 'JupyterWidget'], optional): The enviroment using pygwalker. Default as 'JupyterWidget' | ||
- field_specs (List[FieldSpec], optional): Specifications of some fields. They'll been automatically inferred from `df` if some fields are not specified. | ||
- theme_key ('vega' | 'g2' | 'streamlit'): theme type. | ||
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme. | ||
- spec (str): chart config data. config id, json, remote file url | ||
- use_kernel_calc(bool): Whether to use kernel compute for datas, Default to None, automatically determine whether to use kernel calculation. | ||
- kanaries_api_key (str): kanaries api key, Default to "". | ||
- default_tab (Literal["data", "vis"]): default tab to show. Default to "vis" | ||
""" | ||
if field_specs is None: | ||
field_specs = [] | ||
|
||
source_invoke_code = get_formated_spec_params_code_from_frame( | ||
inspect.stack()[1].frame | ||
) | ||
|
||
widget = _WalkerWidget() | ||
walker = PygWalker( | ||
gid=gid, | ||
dataset=dataset, | ||
field_specs=field_specs, | ||
spec=spec, | ||
source_invoke_code=source_invoke_code, | ||
theme_key=theme_key, | ||
appearance=appearance, | ||
show_cloud_tool=show_cloud_tool, | ||
use_preview=False, | ||
kernel_computation=True, | ||
use_save_tool=True, | ||
gw_mode="explore", | ||
is_export_dataframe=True, | ||
kanaries_api_key=kanaries_api_key, | ||
default_tab=default_tab, | ||
cloud_computation=False, | ||
**kwargs | ||
) | ||
comm = AnywidgetCommunication(walker.gid) | ||
|
||
widget.props = json.dumps(walker._get_props("marimo", [])) | ||
comm.register_widget(widget) | ||
walker._init_callback(comm) | ||
|
||
return mo.ui.anywidget(widget) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Dict, Optional, List | ||
import uuid | ||
import json | ||
|
||
import anywidget | ||
|
||
from .base import BaseCommunication | ||
from pygwalker.utils.encode import DataFrameEncoder | ||
|
||
|
||
class AnywidgetCommunication(BaseCommunication): | ||
"""communication class for anywidget""" | ||
def register_widget(self, widget: anywidget.AnyWidget) -> None: | ||
"""register widget""" | ||
self.widget = widget | ||
self.widget.on_msg(self._on_mesage) | ||
|
||
def send_msg_async(self, action: str, data: Dict[str, Any], rid: Optional[str] = None): | ||
"""send message base on anywidget""" | ||
if rid is None: | ||
rid = uuid.uuid1().hex | ||
msg = { | ||
"gid": self.gid, | ||
"rid": rid, | ||
"action": action, | ||
"data": data | ||
} | ||
self.widget.send({"type": "pyg_response", "data": json.dumps(msg, cls=DataFrameEncoder)}) | ||
|
||
def _on_mesage(self, _: anywidget.AnyWidget, data: Dict[str, Any], buffers: List[Any]): | ||
if data.get("type", "") != "pyg_request": | ||
return | ||
|
||
msg = data["msg"] | ||
action = msg["action"] | ||
rid = msg["rid"] | ||
|
||
if action == "finish_request": | ||
return | ||
|
||
resp = self._receive_msg(action, msg["data"]) | ||
self.send_msg_async("finish_request", resp, rid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters