Skip to content

Commit

Permalink
feat: add pygwalker web api tips in streamlit
Browse files Browse the repository at this point in the history
  • Loading branch information
longxiaofei committed Jul 7, 2024
1 parent 7a846a1 commit b795f29
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pygwalker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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"
__version__ = "0.4.9.1"
__hash__ = __rand_str()

from pygwalker.api.jupyter import walk, render, table
Expand Down
14 changes: 10 additions & 4 deletions pygwalker/communications/streamlit_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import streamlit as st

from pygwalker.utils.encode import DataFrameEncoder
from pygwalker.errors import StreamlitPygwalkerApiError
from .base import BaseCommunication

streamlit_comm_map = {}
Expand Down Expand Up @@ -46,11 +47,16 @@ def post(self, gid: str):
def hack_streamlit_server():
tornado_obj = None
for obj in gc.get_objects():
if isinstance(obj, Application):
tornado_obj = obj
break
try:
if isinstance(obj, Application) and any(("streamlit" in str(rule.target) for rule in obj.wildcard_router.rules)):
tornado_obj = obj
except Exception:
pass

tornado_obj.add_handlers(".*", [(PYGWALKER_API_PATH, PygwalkerHandler)])
if tornado_obj:
tornado_obj.add_handlers(".*", [(PYGWALKER_API_PATH, PygwalkerHandler)])
else:
raise StreamlitPygwalkerApiError()


class StreamlitCommunication(BaseCommunication):
Expand Down
9 changes: 9 additions & 0 deletions pygwalker/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ def __init__(self) -> None:
"The query returned too many data entries, making it difficult for the frontend to render. Please adjust your chart configuration and try again.",
code=ErrorCode.UNKNOWN_ERROR
)


class StreamlitPygwalkerApiError(BaseError):
"""Raised when the config is invalid."""
def __init__(self) -> None:
super().__init__(
"Adding pygwalker web api to streamlit failed. If possible, please report this case to the pygwalker team. Thanks!",
code=ErrorCode.UNKNOWN_ERROR
)

0 comments on commit b795f29

Please sign in to comment.