Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: dispaly in repl #661

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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
6 changes: 4 additions & 2 deletions pygwalker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from pygwalker.services.global_var import GlobalVarManager
from pygwalker.services.kaggle import show_tips_user_kaggle as __show_tips_user_kaggle

__version__ = "0.4.9.13"
__version__ = "0.4.9.14a1"
__hash__ = __rand_str()

from pygwalker.api.jupyter import walk, render, table
from pygwalker.adapter.walk import walk
from pygwalker.adapter.render import render
from pygwalker.adapter.table import 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.
59 changes: 59 additions & 0 deletions pygwalker/adapter/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from pygwalker.adapter.utils import can_open_window, is_notebook
from pygwalker.api import repl, webview, 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
)
elif can_open_window():
return webview.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
)

53 changes: 53 additions & 0 deletions pygwalker/adapter/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pygwalker.adapter.utils import can_open_window, is_notebook
from pygwalker.api import repl, webview, 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
)
elif can_open_window():
return webview.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
)
36 changes: 36 additions & 0 deletions pygwalker/adapter/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def is_notebook() -> bool:
try:
ipython = get_ipython()
shell = ipython.__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
if 'google.colab' in str(ipython):
return True # in Google colab
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter

def can_open_window() -> bool:
import os
import platform

# Check if running in SSH session
if 'SSH_CONNECTION' in os.environ:
return False

system = platform.system()

if system == 'Darwin': # macOS
# macOS can typically always open windows unless in remote session
return True
elif system == 'Linux':
# Check for X11 or Wayland display
return bool(os.environ.get('DISPLAY') or os.environ.get('WAYLAND_DISPLAY'))
elif system == 'Windows':
# Windows can typically always open windows unless in remote session
return True
else:
return False # Unknown OS, assume no GUI capability
94 changes: 94 additions & 0 deletions pygwalker/adapter/walk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from pygwalker.adapter.utils import is_notebook, can_open_window
from pygwalker.api import repl, webview, 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
)
elif can_open_window():
return webview.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
)
Loading
Loading