Skip to content

Commit

Permalink
Add a new VOILA_REQUEST_URL env variable for convenience with Bokeh
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Mar 21, 2023
1 parent 219510d commit 55d30d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions notebooks/bokeh.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"import os\n",
"from urllib.parse import urlparse\n",
"\n",
"url = urlparse(os.environ.get(\"VOILA_SERVER_URL\"))\n",
"url = urlparse(os.environ.get(\"VOILA_REQUEST_URL\"))\n",
"\n",
"show(bkapp, notebook_url=f\"{url.scheme}://{url.netloc}\")\n",
"```"
Expand All @@ -102,7 +102,7 @@
"import os\n",
"from urllib.parse import urlparse\n",
"\n",
"url = urlparse(os.environ.get(\"VOILA_SERVER_URL\"))\n",
"url = urlparse(os.environ.get(\"VOILA_REQUEST_URL\"))\n",
"\n",
"show(bkapp, notebook_url=f\"{url.scheme}://{url.netloc}\")"
]
Expand Down
13 changes: 7 additions & 6 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ async def get_generator(self, path=None):
kernel_id,
) = await self.kernel_manager.get_rendered_notebook(
notebook_name=notebook_path,
extra_kernel_env_variables={
ENV_VARIABLE.VOILA_REQUEST_URL: self.request.full_url()
}
)

RequestInfoSocketHandler.send_updates(
Expand Down Expand Up @@ -198,15 +201,13 @@ def time_out():

return "<script>window.voila_heartbeat()</script>\n"

server_url = self.request.full_url()
# Removing trailing slash
if server_url[-1] == "/":
server_url = server_url[:-1]

kernel_env = {**os.environ, **request_info}
kernel_env[ENV_VARIABLE.VOILA_PREHEAT] = "False"
kernel_env[ENV_VARIABLE.VOILA_BASE_URL] = self.base_url
kernel_env[ENV_VARIABLE.VOILA_SERVER_URL] = server_url
kernel_env[ENV_VARIABLE.VOILA_SERVER_URL] = self.settings.get(
"server_url", "/"
)
kernel_env[ENV_VARIABLE.VOILA_REQUEST_URL] = self.request.full_url()
kernel_env[ENV_VARIABLE.VOILA_APP_PORT] = request_info[
ENV_VARIABLE.SERVER_PORT
]
Expand Down
7 changes: 5 additions & 2 deletions voila/voila_kernel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(self, **kwargs):
self.fill_if_needed(delay=0, notebook_name=str(nb))

async def get_rendered_notebook(
self, notebook_name: str, **kwargs
self, notebook_name: str, extra_kernel_env_variables: dict = {}, **kwargs
) -> Tuple[asyncio.Task, TypeList[str]]:
"""Get the notebook rendering task and the rendered cell.
By setting the `stop_generator` to True, the running task
Expand Down Expand Up @@ -167,7 +167,7 @@ async def get_rendered_notebook(
self.log.info(
"Using pre-heated kernel: %s for %s", kernel_id, notebook_name
)
self.fill_if_needed(delay=None, notebook_name=notebook_name, **kwargs)
self.fill_if_needed(delay=None, notebook_name=notebook_name, extra_kernel_env_variables=extra_kernel_env_variables, **kwargs)

return render_task, renderer.rendered_cache, kernel_id

Expand All @@ -178,6 +178,7 @@ def fill_if_needed(
self,
delay: Union[float, None] = None,
notebook_name: Union[str, None] = None,
extra_kernel_env_variables: dict = {},
**kwargs,
) -> None:
"""Start kernels until the pool is full
Expand Down Expand Up @@ -221,6 +222,8 @@ def fill_if_needed(
for key in kernel_env_variables:
if key not in kernel_env:
kernel_env[key] = kernel_env_variables[key]

kernel_env.update(extra_kernel_env_variables)
kernel_env[ENV_VARIABLE.VOILA_BASE_URL] = self.parent.base_url
kernel_env[ENV_VARIABLE.VOILA_SERVER_URL] = self.parent.server_url
kernel_env[ENV_VARIABLE.VOILA_APP_PORT] = str(self.parent.port)
Expand Down

0 comments on commit 55d30d4

Please sign in to comment.