Skip to main content

No project description provided

Project description

Streamlit GO

PyPI Latest Release

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.5.tar.gz (201.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlitgo-0.1.5-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file streamlitgo-0.1.5.tar.gz.

File metadata

  • Download URL: streamlitgo-0.1.5.tar.gz
  • Upload date:
  • Size: 201.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for streamlitgo-0.1.5.tar.gz
Algorithm Hash digest
SHA256 91b0ca794befb93b960d9dbc6ae1681df661702d93b3ffea93cc5cb11b6e3597
MD5 1414b2a5b25a3898a262bb08490f86b8
BLAKE2b-256 44c127582c55b1061d7895b73e5c2320e18bd802b74e2b1564dc8dec7ae7e2f6

See more details on using hashes here.

File details

Details for the file streamlitgo-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: streamlitgo-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for streamlitgo-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e8bbfffa59129dee2819c9e11e454d4617913b375b5c528ebb892d4bcc8015af
MD5 7608fc98cc25f213a9478e19a5485c31
BLAKE2b-256 4df8508b88c6fda586303d8c4fdb0d2222f483f740e3e29dbf61f8fe213c457b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page