Skip to content

Commit c68b5fa

Browse files
committed
feat: add api to adapte multiple env
1 parent c5a0923 commit c68b5fa

File tree

3 files changed

+193
-2
lines changed

3 files changed

+193
-2
lines changed

pygwalker/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from pygwalker.services.global_var import GlobalVarManager
1111
from pygwalker.services.kaggle import show_tips_user_kaggle as __show_tips_user_kaggle
1212

13-
__version__ = "0.4.9.13"
13+
__version__ = "0.4.9.14a2"
1414
__hash__ = __rand_str()
1515

16-
from pygwalker.api.jupyter import walk, render, table
16+
from pygwalker.api.adapter import walk, render, table
1717
from pygwalker.api.html import to_html
1818
from pygwalker.data_parsers.base import FieldSpec
1919
from pygwalker.api.component import component

pygwalker/api/adapter.py

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
2+
from typing import Union, List, Optional
3+
4+
from typing_extensions import Literal
5+
6+
from pygwalker.data_parsers.base import FieldSpec
7+
from pygwalker.data_parsers.database_parser import Connector
8+
from pygwalker._typing import DataFrame, IAppearance, IThemeKey
9+
from pygwalker.utils.runtime_env import get_current_env
10+
from pygwalker.api import jupyter
11+
from pygwalker.api import webserver
12+
13+
14+
def walk(
15+
dataset: Union[DataFrame, Connector, str],
16+
gid: Union[int, str] = None,
17+
*,
18+
env: Literal['Jupyter', 'JupyterWidget'] = 'JupyterWidget',
19+
field_specs: Optional[List[FieldSpec]] = None,
20+
theme_key: IThemeKey = 'g2',
21+
appearance: IAppearance = 'media',
22+
spec: str = "",
23+
use_kernel_calc: Optional[bool] = None,
24+
kernel_computation: Optional[bool] = None,
25+
cloud_computation: bool = False,
26+
show_cloud_tool: bool = True,
27+
kanaries_api_key: str = "",
28+
default_tab: Literal["data", "vis"] = "vis",
29+
**kwargs
30+
):
31+
"""Walk through pandas.DataFrame df with Graphic Walker
32+
33+
Args:
34+
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
35+
- gid (Union[int, str], optional): GraphicWalker container div's id ('gwalker-{gid}')
36+
37+
Kargs:
38+
- env: (Literal['Jupyter' | 'JupyterWidget'], optional): The enviroment using pygwalker. Default as 'JupyterWidget'
39+
- field_specs (List[FieldSpec], optional): Specifications of some fields. They'll been automatically inferred from `df` if some fields are not specified.
40+
- theme_key ('vega' | 'g2' | 'streamlit'): theme type.
41+
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
42+
- spec (str): chart config data. config id, json, remote file url
43+
- use_kernel_calc(bool): Whether to use kernel compute for datas, Default to None, automatically determine whether to use kernel calculation.
44+
- kanaries_api_key (str): kanaries api key, Default to "".
45+
- default_tab (Literal["data", "vis"]): default tab to show. Default to "vis"
46+
- cloud_computation(bool): Whether to use cloud compute for datas, it upload your data to kanaries cloud. Default to False.
47+
"""
48+
cur_env = get_current_env()
49+
if cur_env == "jupyter":
50+
return jupyter.walk(
51+
dataset,
52+
gid,
53+
env=env,
54+
field_specs=field_specs,
55+
theme_key=theme_key,
56+
appearance=appearance,
57+
spec=spec,
58+
use_kernel_calc=use_kernel_calc,
59+
kernel_computation=kernel_computation,
60+
cloud_computation=cloud_computation,
61+
show_cloud_tool=show_cloud_tool,
62+
kanaries_api_key=kanaries_api_key,
63+
default_tab=default_tab,
64+
**kwargs
65+
)
66+
67+
return webserver.walk(
68+
dataset,
69+
gid,
70+
field_specs=field_specs,
71+
theme_key=theme_key,
72+
appearance=appearance,
73+
spec=spec,
74+
kernel_computation=kernel_computation,
75+
cloud_computation=cloud_computation,
76+
show_cloud_tool=show_cloud_tool,
77+
kanaries_api_key=kanaries_api_key,
78+
default_tab=default_tab,
79+
**kwargs
80+
)
81+
82+
83+
84+
def render(
85+
dataset: Union[DataFrame, Connector, str],
86+
spec: str,
87+
*,
88+
theme_key: IThemeKey = 'g2',
89+
appearance: IAppearance = 'media',
90+
kernel_computation: Optional[bool] = None,
91+
kanaries_api_key: str = "",
92+
**kwargs
93+
):
94+
"""
95+
Args:
96+
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
97+
- spec (str): chart config data. config id, json, remote file url
98+
99+
Kargs:
100+
- theme_key ('vega' | 'g2'): theme type.
101+
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
102+
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
103+
- kanaries_api_key (str): kanaries api key, Default to "".
104+
"""
105+
cur_env = get_current_env()
106+
if cur_env == "jupyter":
107+
return jupyter.render(
108+
dataset,
109+
spec,
110+
theme_key=theme_key,
111+
appearance=appearance,
112+
kernel_computation=kernel_computation,
113+
kanaries_api_key=kanaries_api_key,
114+
**kwargs
115+
)
116+
117+
return webserver.render(
118+
dataset,
119+
spec,
120+
theme_key=theme_key,
121+
appearance=appearance,
122+
kernel_computation=kernel_computation,
123+
kanaries_api_key=kanaries_api_key,
124+
**kwargs
125+
)
126+
127+
128+
129+
def table(
130+
dataset: Union[DataFrame, Connector, str],
131+
*,
132+
theme_key: IThemeKey = 'g2',
133+
appearance: IAppearance = 'media',
134+
kernel_computation: Optional[bool] = None,
135+
kanaries_api_key: str = "",
136+
**kwargs
137+
):
138+
"""
139+
Args:
140+
- dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataframe.
141+
142+
Kargs:
143+
- theme_key ('vega' | 'g2'): theme type.
144+
- appearance (Literal['media' | 'light' | 'dark']): 'media': auto detect OS theme.
145+
- kernel_computation(bool): Whether to use kernel compute for datas, Default to None.
146+
- kanaries_api_key (str): kanaries api key, Default to "".
147+
"""
148+
cur_env = get_current_env()
149+
if cur_env == "jupyter":
150+
return jupyter.table(
151+
dataset,
152+
theme_key=theme_key,
153+
appearance=appearance,
154+
kernel_computation=kernel_computation,
155+
kanaries_api_key=kanaries_api_key,
156+
**kwargs
157+
)
158+
159+
return webserver.table(
160+
dataset,
161+
theme_key=theme_key,
162+
appearance=appearance,
163+
kernel_computation=kernel_computation,
164+
kanaries_api_key=kanaries_api_key,
165+
**kwargs
166+
)

pygwalker/utils/runtime_env.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing_extensions import Literal
2+
3+
4+
def _is_jupyter() -> bool:
5+
try:
6+
from IPython import get_ipython
7+
ip = get_ipython()
8+
if ip is None:
9+
return False
10+
return ip.has_trait('kernel')
11+
except Exception:
12+
return False
13+
14+
15+
def get_current_env() -> Literal["jupyter", "other"]:
16+
"""
17+
Get the current environment.
18+
19+
Returns:
20+
Literal["jupyter", "other"]: The current environment.
21+
"""
22+
if _is_jupyter():
23+
return "jupyter"
24+
else:
25+
return "other"

0 commit comments

Comments
 (0)