Skip to content

Commit

Permalink
feat: dispaly in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
islxyqwe committed Dec 2, 2024
1 parent 8335828 commit af99e0f
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 1 deletion.
2 changes: 2 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ dependencies:
- python>=3.5
- pip
- polars
- bottle
- pywebview
- pip:
- pygwalker>=0.1
2 changes: 1 addition & 1 deletion pygwalker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
__version__ = "0.4.9.13"
__hash__ = __rand_str()

from pygwalker.api.jupyter import walk, render, table
from pygwalker.adapter import walk, render, table
from pygwalker.api.html import to_html
from pygwalker.data_parsers.base import FieldSpec
from pygwalker.api.component import component
Expand Down
Empty file added pygwalker/adapter/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions pygwalker/adapter/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pygwalker.adapter.utils import is_notebook
import pygwalker.api.repl as repl
import pygwalker.api.jupyter as jupyter
from typing import Optional, Union
from pygwalker._typing import DataFrame, IAppearance, IThemeKey
from pygwalker.data_parsers.database_parser import Connector

def render(
dataset: Union[DataFrame, Connector, str],
spec: str,
*,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
kernel_computation: Optional[bool] = None,
kanaries_api_key: str = "",
**kwargs
):
"""
Args:
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
- spec (str): chart config data. config id, json, remote file url
Kargs:
- theme_key ('vega' | 'g2'): theme type.
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
- kanaries_api_key (str): kanaries api key, Default to "".
"""

if is_notebook():
return jupyter.render(
dataset,
spec,
theme_key=theme_key,
appearance=appearance,
kernel_computation=kernel_computation,
kanaries_api_key=kanaries_api_key,
**kwargs
)
else:
return repl.render(
dataset,
spec,
theme_key=theme_key,
appearance=appearance,
kernel_computation=kernel_computation,
kanaries_api_key=kanaries_api_key,
**kwargs
)

45 changes: 45 additions & 0 deletions pygwalker/adapter/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pygwalker.adapter.utils import is_notebook
import pygwalker.api.repl as repl
import pygwalker.api.jupyter as jupyter
from typing import Optional, Union
from pygwalker._typing import DataFrame, IAppearance, IThemeKey
from pygwalker.data_parsers.database_parser import Connector

def table(
dataset: Union[DataFrame, Connector, str],
*,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
kernel_computation: Optional[bool] = None,
kanaries_api_key: str = "",
**kwargs
):
"""
Args:
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
Kargs:
- theme_key ('vega' | 'g2'): theme type.
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
- kanaries_api_key (str): kanaries api key, Default to "".
"""

if is_notebook():
return jupyter.table(
dataset,
theme_key=theme_key,
appearance=appearance,
kernel_computation=kernel_computation,
kanaries_api_key=kanaries_api_key,
**kwargs
)
else:
return repl.table(
dataset,
theme_key=theme_key,
appearance=appearance,
kernel_computation=kernel_computation,
kanaries_api_key=kanaries_api_key,
**kwargs
)
11 changes: 11 additions & 0 deletions pygwalker/adapter/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def is_notebook() -> bool:
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
78 changes: 78 additions & 0 deletions pygwalker/adapter/walk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from pygwalker.adapter.utils import is_notebook
import pygwalker.api.repl as repl
import pygwalker.api.jupyter as jupyter
from typing import List, Optional, Union
from pygwalker._typing import DataFrame, IAppearance, IThemeKey
from pygwalker.data_parsers.database_parser import Connector
from typing_extensions import Literal
from pygwalker.data_parsers.base import FieldSpec


def walk(
dataset: Union[DataFrame, Connector, str],
gid: Union[int, str] = None,
*,
env: Literal['Jupyter', 'JupyterWidget'] = 'JupyterWidget',
field_specs: Optional[List[FieldSpec]] = None,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
spec: str = "",
use_kernel_calc: Optional[bool] = None,
kernel_computation: Optional[bool] = None,
cloud_computation: bool = False,
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"
- cloud_computation(bool): Whether to use cloud compute for datas, it upload your data to kanaries cloud. Default to False.
"""
if is_notebook():
return jupyter.walk(
dataset,
gid,
env=env,
field_specs=field_specs,
theme_key=theme_key,
appearance=appearance,
spec=spec,
use_kernel_calc=use_kernel_calc,
kernel_computation=kernel_computation,
cloud_computation=cloud_computation,
show_cloud_tool=show_cloud_tool,
kanaries_api_key=kanaries_api_key,
default_tab=default_tab,
**kwargs
)
else:
return repl.walk(
dataset,
gid,
env=env,
field_specs=field_specs,
theme_key=theme_key,
appearance=appearance,
spec=spec,
use_kernel_calc=use_kernel_calc,
kernel_computation=kernel_computation,
cloud_computation=cloud_computation,
show_cloud_tool=show_cloud_tool,
kanaries_api_key=kanaries_api_key,
default_tab=default_tab,
**kwargs
)
185 changes: 185 additions & 0 deletions pygwalker/api/repl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from typing import Union, List, Optional, Dict, Any
from pygwalker.communications.base import BaseCommunication

from typing_extensions import Literal
import json
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 bottle import Bottle, request, response
from pygwalker.utils.check_walker_params import check_expired_params
from pygwalker.utils import fallback_value
from pygwalker.utils.encode import DataFrameEncoder
import webview

def start_webview(walker: PygWalker):
comm = BaseCommunication(walker.gid)
walker._init_callback(comm)
app = Bottle()
# api path and html path need to have the same prefix
@app.route("/comm/", methods=["POST"])
@app.route("/comm/<gid>", methods=["POST"])
def pygwalker_comm(gid):
payload = request.json
comm_obj = walker.comm
result = comm_obj._receive_msg(payload["action"], payload["data"])
response.content_type = 'application/json'
return json.dumps(result, cls=DataFrameEncoder)


# api path and html path need to have the same prefix
@app.route("/")
def pyg_html():
props = walker._get_props("web_server")
props["communicationUrl"] = "comm"
html = walker._get_render_iframe(props, True)
return html

webview.create_window('Pygwalker', app)
webview.start(debug=True)


def walk(
dataset: Union[DataFrame, Connector, str],
gid: Union[int, str] = None,
*,
env: Literal['Jupyter', 'JupyterWidget'] = 'JupyterWidget',
field_specs: Optional[List[FieldSpec]] = None,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
spec: str = "",
use_kernel_calc: Optional[bool] = None,
kernel_computation: Optional[bool] = None,
cloud_computation: bool = False,
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"
- cloud_computation(bool): Whether to use cloud compute for datas, it upload your data to kanaries cloud. Default to False.
"""
check_expired_params(kwargs)

if field_specs is None:
field_specs = []

walker = PygWalker(
gid=gid,
dataset=dataset,
field_specs=field_specs,
spec=spec,
source_invoke_code="",
theme_key=theme_key,
appearance=appearance,
show_cloud_tool=show_cloud_tool,
use_preview=False,
kernel_computation=(isinstance(dataset, (Connector, str)) or fallback_value(kernel_computation, use_kernel_calc)),
is_export_dataframe=False,
use_save_tool=False,
kanaries_api_key=kanaries_api_key,
default_tab=default_tab,
cloud_computation=cloud_computation,
gw_mode="explore",
)
start_webview(walker)

def render(
dataset: Union[DataFrame, Connector, str],
spec: str,
*,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
kernel_computation: Optional[bool] = None,
kanaries_api_key: str = "",
**kwargs
):
"""
Args:
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
- spec (str): chart config data. config id, json, remote file url
Kargs:
- theme_key ('vega' | 'g2'): theme type.
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
- kanaries_api_key (str): kanaries api key, Default to "".
"""

walker = PygWalker(
gid=None,
dataset=dataset,
field_specs=[],
spec=spec,
source_invoke_code="",
theme_key=theme_key,
appearance=appearance,
show_cloud_tool=False,
use_preview=False,
kernel_computation=isinstance(dataset, (Connector, str)) or kernel_computation,
use_save_tool=False,
gw_mode="filter_renderer",
is_export_dataframe=True,
kanaries_api_key=kanaries_api_key,
default_tab="vis",
cloud_computation=False,
**kwargs
)

start_webview(walker)

def table(
dataset: Union[DataFrame, Connector, str],
*,
theme_key: IThemeKey = 'g2',
appearance: IAppearance = 'media',
kernel_computation: Optional[bool] = None,
kanaries_api_key: str = "",
**kwargs
):
"""
Args:
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
Kargs:
- theme_key ('vega' | 'g2'): theme type.
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
- kanaries_api_key (str): kanaries api key, Default to "".
"""
walker = PygWalker(
gid=None,
dataset=dataset,
field_specs=[],
spec="",
source_invoke_code="",
theme_key=theme_key,
appearance=appearance,
show_cloud_tool=False,
use_preview=False,
kernel_computation=isinstance(dataset, (Connector, str)) or kernel_computation,
use_save_tool=False,
gw_mode="table",
is_export_dataframe=True,
kanaries_api_key=kanaries_api_key,
default_tab="vis",
cloud_computation=False,
**kwargs
)

start_webview(walker)
Loading

0 comments on commit af99e0f

Please sign in to comment.