No project description provided
Project description
Streamlit GO
Package for loading a script before streamlit server start
This package will load stgo.py
in current working directory before the streamlit server starts. You can use this to patch the streamlit server or to load some data before the server starts!
For example:
- add some code before each page run
- add a custom endpoint to the streamlit server
- get current authenticated user info by custom header
Installation
pip install streamlitgo
Usage
Just create your own stgo.py
in your working directory and use streamlitgo
instead of streamlit
in your command line.
streamlitgo run your_script.py
Example
This example shows how to log each script rerun with the user email and remote ip.
stgo.py
:
import signal
import typing as T
import streamlit.runtime.app_session
import streamlit.web.bootstrap
import streamlit.web.server
from streamlit.logger import get_logger
from streamlit.runtime.scriptrunner.script_cache import ScriptCache
from streamlit.runtime.scriptrunner.script_run_context import ScriptRunContext
from streamlit.user_info import _get_user_info
from streamlit.web.server import Server
from streamlit.web.server.browser_websocket_handler import BrowserWebSocketHandler
logger = get_logger(__name__)
class MyScriptCache(ScriptCache):
def get_bytecode(self, script_path: str) -> T.Any:
user = _get_user_info()
email = user.get("email", "")
remote_ip = user.get("remote_ip", "")
logger.info(f"{email} [{remote_ip}] access script {script_path}")
return super().get_bytecode(script_path)
streamlit.runtime.runtime.ScriptCache = MyScriptCache
class MyBrowserWebSocketHandler(BrowserWebSocketHandler):
def open(self, *args, **kwargs) -> T.Awaitable[None] | None:
ret = super().open(*args, **kwargs)
session = self._runtime._session_mgr.get_session_info(self._session_id).session
email = self.request.headers.get("x-auth-request-user", "bob@Alice.com")
remote_ip = self.request.headers.get("X-Real-IP", "192.168.1.1")
user = session._user_info
user["email"] = email
user["remote_ip"] = remote_ip
return ret
class StreamlitServer(Server):
def _create_app(self):
app = super()._create_app()
rules = app.wildcard_router.rules
for rule in rules:
if issubclass(rule.target, BrowserWebSocketHandler):
rule.target = MyBrowserWebSocketHandler
break
return app
streamlit.web.bootstrap.Server = StreamlitServer
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
streamlitgo-0.1.3.tar.gz
(2.6 kB
view details)
Built Distribution
File details
Details for the file streamlitgo-0.1.3.tar.gz
.
File metadata
- Download URL: streamlitgo-0.1.3.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6be4a7aeae8bdbff8ba3f4753ed070c3d857e92f50fbb72a441def74e521ef36 |
|
MD5 | aaff0f2d0d2559824861a09fc9a81f6d |
|
BLAKE2b-256 | 77595e3c2de6f31c7aab88184299c5ac50ffcaab0aabd88a47f06ba7d08f21fc |
File details
Details for the file streamlitgo-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: streamlitgo-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7eab1830816092b2a2f1adafdc9d3fe7e59bfa64643205d8f4ccf393178b87eb |
|
MD5 | 7a45d3ee452e921ee2484b6e69d3d895 |
|
BLAKE2b-256 | 1dc2d36192d1d84aceab58fa1a5148955a536b931f3fe55d79d44b463652b001 |