Skip to content

Commit

Permalink
v1 of patch server
Browse files Browse the repository at this point in the history
with heavy heart I write these lines.
on one side, the customer, the other security.
  • Loading branch information
mike dupont committed Sep 16, 2023
1 parent 2103b9b commit ab82b10
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 132 deletions.
24 changes: 24 additions & 0 deletions backups/inject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# #begin inject

# from streamlit.web.server.server import Server
# server_create_app_backup = Server._create_app

# def server_create_app_hack(self):
# app = server_create_app_backup(self)
# print("PATCH",app)


# return app
# Server._create_app=server_create_app_hack


#begin inject
from streamlit.web.server.server import Server
import streamlit as st
server_create_app_backup = Server._create_app
def server_create_app_hack(self):
app = server_create_app_backup(self)
print("PATCH",app)
st.write(app)
return app
Server._create_app=server_create_app_hack
12 changes: 5 additions & 7 deletions backups/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ async def start(self) -> None:
LOGGER.debug("Starting server...")

app = self._create_app()
print("APP",app)
start_listening(app)

port = config.get_option("server.port")
Expand All @@ -273,7 +272,7 @@ def stopped(self) -> Awaitable[None]:
def _create_app(self) -> tornado.web.Application:
"""Create our tornado web app."""
base = config.get_option("server.baseUrlPath")
#raise Exception("err")

routes: List[Any] = [
(
make_url_path_regex(base, STREAM_ENDPOINT),
Expand Down Expand Up @@ -399,10 +398,9 @@ def stop(self) -> None:


def _set_tornado_log_levels() -> None:
pass
#if not config.get_option("global.developmentMode"):
if not config.get_option("global.developmentMode"):
# Hide logs unless they're super important.
# Example of stuff we don't care about: 404 about .js.map files.
logging.getLogger("tornado.access").setLevel(logging.DEBUG)
logging.getLogger("tornado.application").setLevel(logging.DEBUG)
logging.getLogger("tornado.general").setLevel(logging.DEBUG)
logging.getLogger("tornado.access").setLevel(logging.ERROR)
logging.getLogger("tornado.application").setLevel(logging.ERROR)
logging.getLogger("tornado.general").setLevel(logging.ERROR)
30 changes: 30 additions & 0 deletions src/patch_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import streamlit.web.server.server
import inspect
import os.path
target = "backups"
#st.write(inspect.getsourcefile(streamlit.web.server.server))
INJECT="""
import streamlit.web.server.inject
"""

def patch_server():
#copy a backup of server code
target = inspect.getsourcefile(streamlit.web.server.server)
inject_target = target.replace("/server.py","/inject.py",)
backups = "backups"
backup = os.path.join(backups,"server_old.py")
inject_source = os.path.join(backups,"inject.py")
if not os.path.exists(backup):
with open(backup,"w") as fo:
with open(target,"r") as fi:
fo.write(fi.read())
# patch the file
with open(backup,"r") as fi:
with open(target,"w") as fo:
fo.write(fi.read())
fo.write(INJECT)

# write the injection target, safer
with open(inject_target,"w") as fo:
with open(inject_source,"r") as fi:
fo.write(fi.read())
131 changes: 6 additions & 125 deletions src/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
import streamlit.runtime.scriptrunner.script_runner
import streamlit.web.server.server

import streamlit.web.server.routes

import tornado.httpserver
Expand All @@ -28,130 +28,7 @@
import tornado.websocket
import traceback
import inspect
st.write(inspect.getsourcefile(streamlit.web.server.server))

target = "backups"
if not os.path.exists(target):
os.mkdir(target)
#copy abackup of server
with open(os.path.join(target,"server.py"),"w") as fo:
with open(inspect.getsourcefile(streamlit.web.server.server),"r") as fi:
fo.write(fi.read())

ctx =streamlit.runtime.scriptrunner.script_runner.get_script_run_ctx()

# st.write("CTX",ctx)
# st.write("CTX",str(ctx))
# st.write("Sess",dir(ctx))
# for x in dir(ctx):
# st.write("DEBUG",x,getattr(ctx,x,"None"))



# ctxScriptRunContextScriptRunContext(session_id='6ab6368d-3bf6-492e-a2d0-d8d3a5733e71', _enqueue=<bound method ScriptRunner._enqueue_forward_msg of ScriptRunner(_session_id='6ab6368d-3bf6-492e-a2d0-d8d3a5733e71', _main_script_path='./src/streamlit_app.py', _uploaded_file_mgr=MemoryUploadedFileManager(endpoint='/_stcore...
# A context object that contains data for a "script run" - that is,
# data that's scoped to a single ScriptRunner execution (and therefore also
# scoped to a single connected "session").

# ScriptRunContext is used internally by virtually every `st.foo()` function.
# It is accessed only from the script thread that's created by ScriptRunner.

# Streamlit code typically retrieves the active ScriptRunContext via the
# `get_script_run_ctx` function.
# command_tracking_deactivatedbool True
# cursorsdict {}
# dg_stacklist []
# form_ids_this_runset set()
# gather_usage_statsbool True
# page_script_hashstr '04d9c6d3425fdb18dfa2143151c97767'
# query_stringstr ''
# session_idstr '6ab6368d-3bf6-492e-a2d0-d8d3a5733e71'
# session_stateSafeSessionState Thread-safe wrapper around SessionState.
# tracked_commandslist [name: "write" args { k: "1" t: "streamlit.runtime.scriptrunner.script_run_context.ScriptRunContext" p: 1 } ]
# tracked_commands_counterCounter Counter({'write': 1})
# uploaded_file_mgrMemoryUploadedFileManager MemoryUploadedFileManager(endpoint='/_stcore/upload_file')
# user_infodict {'email': 'test@localhost.com'}
# widget_ids_this_runset set()
# widget_user_keys_this_runset set()
# enqueuemethod Enqueue a ForwardMsg for this context's session.
# on_script_startmethod No docs available
# resetmethod No docs available
# ScriptRunContext(session_id='6ab6368d-3bf6-492e-a2d0-d8d3a5733e71', _enqueue=<bound method ScriptRunner._enqueue_forward_msg of ScriptRunner(_session_id='6ab6368d-3bf6-492e-a2d0-d8d3a5733e71', _main_script_path='./src/streamlit_app.py', _uploaded_file_mgr=MemoryUploadedFileManager(endpoint='/_stcore/upload_file'), _script_cache=<streamlit.runtime.scriptrunner.script_cache.ScriptCache object at 0x7f305f154d90>, _user_info={'email': 'test@localhost.com'}, _client_state=, _session_state=<streamlit.runtime.state.safe_session_state.SafeSessionState object at 0x7f305cc78670>, _requests=<streamlit.runtime.scriptrunner.script_requests.ScriptRequests object at 0x7f304dc0b2e0>, on_event=<blinker.base.Signal object at 0x7f304dc0ba30>, _execing=True, _script_thread=<Thread(ScriptRunner.scriptThread, started 139845467305536)>)>, query_string='', session_state=<streamlit.runtime.state.safe_session_state.SafeSessionState object at 0x7f305cc78670>, uploaded_file_mgr=MemoryUploadedFileManager(endpoint='/_stcore/upload_file'), page_script_hash='04d9c6d3425fdb18dfa2143151c97767', user_info={'email': 'test@localhost.com'}, gather_usage_stats=True, command_tracking_deactivated=False, tracked_commands=[name: "write" args { k: "1" t: "streamlit.runtime.scriptrunner.script_run_context.ScriptRunContext" p: 1 } time: 5345 ], tracked_commands_counter=Counter({'write': 1}), _set_page_config_allowed=False, _has_script_started=True, widget_ids_this_run=set(), widget_user_keys_this_run=set(), form_ids_this_run=set(), cursors={0: RunningCursor(_parent_path=(), _index=1)}, dg_stack=[])

#st.write("Sess STate",ctx.session_state._state)
#st.write("Sess STate",dir(ctx.session_state._state))
#st.write("Sess",dir(ctx.session_state))


values = {}


#st.write("current",tornado.ioloop.IOLoop.current())

#import pdb
#pdb.set_trace()

# for x in inspect.stack():
# st.write("CALLBACK",str(x))
# #st.write(dir(x))
# st.write("CALLBACK1",x.frame)
# st.write("CALLBACK2",x.frame.f_globals)
# st.write("CALLBACK3",x.code_context)

#for line in traceback.format_stack():
# st.write(line.strip())
# st.write(line)

_io_loops= {}
_io_loop = tornado.ioloop.IOLoop.current()
#st.write("current",type(_io_loop).__name__)
if _io_loop not in _io_loops:
_io_loops[_io_loop] = True
#st.write("started ")

asyncio_loop = _io_loop.asyncio_loop # type: ignore

#st.write("asyncio_loop ",asyncio_loop)
tasks = asyncio.all_tasks(asyncio_loop) # type: ignore

asyncio_loop.set_debug(True)
#for x in dir(asyncio_loop):
# st.write(x)
#st.write("asyncio_loop tasks", tasks)
#if hasattr(asyncio, "all_tasks"): # py37

# else:
# tasks = asyncio.Task.all_tasks(asyncio_loop)


#modify_times = {} # type: Dict[str, float]
#callback = functools.partial(_reload_on_update, modify_times)
#scheduler = ioloop.PeriodicCallback(callback, check_time)
#scheduler.start()


# for x in list(globals()):
# v = globals()[x]

# if isinstance(v, types.ModuleType):
# values[x] = globals()[x]


st.write("server")
#st.dataframe(dir(streamlit.web.server.server))
for module in values:
st.write(module)
#st.write(help(module))
#for x in values[module]:
# v = values[module][x]
# st.write(module,x,v)
#st.dataframe(dir(module))

#st.dataframe(dir(streamlit))


raise Exception("test")

import patch_server
oparams = st.experimental_get_query_params()
params = {
x: oparams[x][0] for x in oparams
Expand Down Expand Up @@ -207,6 +84,10 @@ def get_input(input_id):
"one-input": OneInputs(),
}

if params.get("patch-server",False):
st.write("patching server")
patch_server.patch_server()

app_args = dict(
mode = st.text_input("Mode", help="Mode to use", key="mode",value=params.get("mode","concept-inputs")),
#st.selectbox("mode",
Expand Down

0 comments on commit ab82b10

Please sign in to comment.