Skip to content

Commit

Permalink
Add experimental get|set_url capability
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoooyue committed Apr 9, 2020
1 parent 3d15636 commit 5e90067
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export class App extends PureComponent<Props, State> {
uploadReportProgress: (progress: string | number) =>
this.handleUploadReportProgress(progress),
reportUploaded: (url: string) => this.handleReportUploaded(url),
newUrl: (new_url: string) => this.handleNewUrlChange(new_url),
})
} catch (err) {
logError(err)
Expand Down Expand Up @@ -339,6 +340,11 @@ export class App extends PureComponent<Props, State> {
})

this.handleSessionStateChanged(sessionState)

// send current URL to server
const backMsg = new BackMsg({ urlInfo: window.location.href })
backMsg.type = "urlInfo"
this.sendBackMsg(backMsg)
}

/**
Expand Down Expand Up @@ -454,6 +460,14 @@ export class App extends PureComponent<Props, State> {
}
}

/**
* Handler for ForwardMsg.newUrl messages
* @param newUrl string
*/
handleNewUrlChange = (newUrl: string): void => {
window.history.pushState({}, "", newUrl ? newUrl : "/")
}

/**
* Handler for ForwardMsg.reportFinished messages
* @param status the ReportFinishedStatus that the report finished with
Expand Down
2 changes: 2 additions & 0 deletions lib/streamlit/ReportSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def __init__(self, ioloop, script_path, command_line, uploaded_file_manager):
self._script_request_queue = ScriptRequestQueue()

self._scriptrunner = None
self.base_url = None
self.query_url = ""

LOGGER.debug("ReportSession initialized (id=%s)", self.id)

Expand Down
37 changes: 37 additions & 0 deletions lib/streamlit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
from streamlit.DeltaGenerator import DeltaGenerator as _DeltaGenerator
from streamlit.ReportThread import add_report_ctx as _add_report_ctx
from streamlit.ReportThread import get_report_ctx as _get_report_ctx
from streamlit.server.Server import Server
from streamlit.proto.ForwardMsg_pb2 import ForwardMsg
from streamlit.errors import StreamlitAPIException
from streamlit.proto import BlockPath_pb2 as _BlockPath_pb2
from streamlit.util import functools_wraps as _functools_wraps
Expand Down Expand Up @@ -580,6 +582,38 @@ def _transparent_write(*args):
return args


def _get_url():
"""
this should just return the url that the current session is running on
"""
ctx = _get_report_ctx()
session_infos = Server.get_current()._session_info_by_id
for socket_handler, session_info in session_infos.items():
if session_info.session.enqueue == ctx.enqueue:
session = session_info.session
if not session.base_url:
raise StreamlitAPIException(
"Something is wrong when fetching current browser URL. Please try again."
)
else:
return session.base_url + str(session.query_url)


def _set_url(url_to_set):
"""
this should just append/replace url_to_set to the current session's existing hostname.
"""
ctx = _get_report_ctx()
session_infos = Server.get_current()._session_info_by_id
for socket_handler, session_info in session_infos.items():
if session_info.session.enqueue == ctx.enqueue:
session = session_info.session
newUrlMsg = ForwardMsg()
newUrlMsg.new_url = url_to_set
session._report.enqueue(newUrlMsg)
session.query_url = url_to_set


# We want to show a warning when the user runs a Streamlit script without
# 'streamlit run', but we need to make sure the warning appears only once no
# matter how many times __init__ gets loaded.
Expand Down Expand Up @@ -693,6 +727,9 @@ class _ExperimentalNamespace(object):
# Add experimental features here. For example:
# foo = _foo
show = _show
get_url = _get_url
set_url = _set_url



beta = _BetaNamespace
Expand Down
3 changes: 3 additions & 0 deletions lib/streamlit/server/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ def on_message(self, payload):
"Client tried to close connection when "
"not in development mode"
)
elif msg_type == "url_info":
self._session.base_url = msg.url_info
LOGGER.debug("Successfully set base URL :\n%s", msg.url_info)
else:
LOGGER.warning('No handler for "%s"', msg_type)

Expand Down
3 changes: 3 additions & 0 deletions proto/streamlit/proto/BackMsg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ message BackMsg {

// Set to true to ask the server to close the connection
bool close_connection = 10;

// Request to pass current URL to backend
string url_info = 11;
}
}
3 changes: 3 additions & 0 deletions proto/streamlit/proto/ForwardMsg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ message ForwardMsg {
// for this one. If the client does not have the referenced message
// in its cache, it can retrieve it from the server.
string ref_hash = 11;

// new_url for curious users to set params
string new_url = 12;
}
}

Expand Down

0 comments on commit 5e90067

Please sign in to comment.