-
Notifications
You must be signed in to change notification settings - Fork 524
Add support for query variables in preheat kernel mode #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SylvainCorlay
merged 11 commits into
voila-dashboards:main
from
trungleduc:ft/support-query-variable
Oct 14, 2021
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4f903ef
POC query string
trungleduc a454d17
[wip] get query variable from websocket endpoint
trungleduc a703af8
Move environment variables to `ENV_VARIABLE`
trungleduc 6fad83b
Lint code
trungleduc 1ddbb84
Add docstring
trungleduc 49261b6
update test
trungleduc aa69a40
Add documentation for partially pre-render notebook
trungleduc 228c11f
Start ws connection in another thread
trungleduc 8fb6655
Apply suggestions from code review
trungleduc 51a65af
Rename helper to `get_query_string`
trungleduc b23e05c
Allow multiple `get_query_string` calls
trungleduc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from tornado.websocket import WebSocketHandler | ||
import logging | ||
from typing import Dict | ||
|
||
|
||
class QueryStringSocketHandler(WebSocketHandler): | ||
"""A websocket handler used to provide the query string | ||
assocciated with kernel ids in preheat kernel mode. | ||
|
||
Class variables | ||
--------------- | ||
- _waiters : A dictionary which holds the `websocket` connection | ||
assocciated with the kernel id. | ||
|
||
- cache : A dictionary which holds the query string assocciated | ||
with the kernel id. | ||
""" | ||
_waiters = dict() | ||
_cache = dict() | ||
|
||
def open(self, kernel_id: str) -> None: | ||
"""Create a new websocket connection, this connection is | ||
identified by the kernel id. | ||
|
||
Args: | ||
kernel_id (str): Kernel id used by the notebook when it opens | ||
the websocket connection. | ||
""" | ||
QueryStringSocketHandler._waiters[kernel_id] = self | ||
if kernel_id in self._cache: | ||
self.write_message(self._cache[kernel_id]) | ||
|
||
def on_close(self) -> None: | ||
for k_id, waiter in QueryStringSocketHandler._waiters.items(): | ||
if waiter == self: | ||
break | ||
del QueryStringSocketHandler._waiters[k_id] | ||
|
||
@classmethod | ||
def send_updates(cls: 'QueryStringSocketHandler', msg: Dict) -> None: | ||
"""Class method used to dispath the query string to the waiting | ||
notebook. This method is called in `VoilaHandler` when the query | ||
string becomes available. | ||
If this method is called before the opening of websocket connection, | ||
`msg` is stored in `_cache0` and the message will be dispatched when | ||
a notebook with coresponding kernel id is connected. | ||
|
||
Args: | ||
- msg (Dict): this dictionary contains the `kernel_id` to identify | ||
the waiting notebook and `payload` is the query string. | ||
""" | ||
kernel_id = msg['kernel_id'] | ||
payload = msg['payload'] | ||
waiter = cls._waiters.get(kernel_id, None) | ||
if waiter is not None: | ||
try: | ||
waiter.write_message(payload) | ||
except Exception: | ||
logging.error("Error sending message", exc_info=True) | ||
else: | ||
cls._cache[kernel_id] = payload |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.