|
| 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 | + ) |
0 commit comments