Skip to content

Commit b795f29

Browse files
committed
feat: add pygwalker web api tips in streamlit
1 parent 7a846a1 commit b795f29

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

pygwalker/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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+
__version__ = "0.4.9.1"
1414
__hash__ = __rand_str()
1515

1616
from pygwalker.api.jupyter import walk, render, table

pygwalker/communications/streamlit_comm.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import streamlit as st
99

1010
from pygwalker.utils.encode import DataFrameEncoder
11+
from pygwalker.errors import StreamlitPygwalkerApiError
1112
from .base import BaseCommunication
1213

1314
streamlit_comm_map = {}
@@ -46,11 +47,16 @@ def post(self, gid: str):
4647
def hack_streamlit_server():
4748
tornado_obj = None
4849
for obj in gc.get_objects():
49-
if isinstance(obj, Application):
50-
tornado_obj = obj
51-
break
50+
try:
51+
if isinstance(obj, Application) and any(("streamlit" in str(rule.target) for rule in obj.wildcard_router.rules)):
52+
tornado_obj = obj
53+
except Exception:
54+
pass
5255

53-
tornado_obj.add_handlers(".*", [(PYGWALKER_API_PATH, PygwalkerHandler)])
56+
if tornado_obj:
57+
tornado_obj.add_handlers(".*", [(PYGWALKER_API_PATH, PygwalkerHandler)])
58+
else:
59+
raise StreamlitPygwalkerApiError()
5460

5561

5662
class StreamlitCommunication(BaseCommunication):

pygwalker/errors.py

+9
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ def __init__(self) -> None:
5050
"The query returned too many data entries, making it difficult for the frontend to render. Please adjust your chart configuration and try again.",
5151
code=ErrorCode.UNKNOWN_ERROR
5252
)
53+
54+
55+
class StreamlitPygwalkerApiError(BaseError):
56+
"""Raised when the config is invalid."""
57+
def __init__(self) -> None:
58+
super().__init__(
59+
"Adding pygwalker web api to streamlit failed. If possible, please report this case to the pygwalker team. Thanks!",
60+
code=ErrorCode.UNKNOWN_ERROR
61+
)

0 commit comments

Comments
 (0)