Skip to content
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

Fix default VOILA_WS_BASE_URL value in preheating mode #1141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/app/preheat_with_query_string_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest
import time
import asyncio
import os


@pytest.fixture(params=[['--Voila.base_url="/base/"'], []])
def using_base_url(request):
return request.param


@pytest.fixture(params=[['--Voila.server_url="/server/"'], []])
def using_server_url(request):
return request.param


@pytest.fixture()
def voila_args_extra(using_server_url, using_base_url):
return using_server_url + using_base_url


@pytest.fixture
def preheat_mode(http_server_port):
os.environ['VOILA_APP_PORT'] = str(http_server_port[-1])
return True


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(
notebook_directory, 'preheat', 'get_query_string.ipynb'
)


NOTEBOOK_EXECUTION_TIME = 2
TIME_THRESHOLD = NOTEBOOK_EXECUTION_TIME


async def send_request(sc, url, wait=0):
await asyncio.sleep(wait)
real_time = time.time()
response = await sc.fetch(url)
real_time = time.time() - real_time
html_text = response.body.decode('utf-8')
return real_time, html_text


async def test_request_with_query(
http_server_client, base_url, using_base_url, using_server_url
):
"""
We sent request with query parameter, `get_query_string` should
return value.
"""
if len(using_server_url) > 0:
url = '/server/?foo=bar'
else:
if len(using_base_url) > 0:
url = '/base/?foo=bar'
else:
url = f'{base_url}?foo=bar'
_, html_text = await send_request(
sc=http_server_client, url=url, wait=NOTEBOOK_EXECUTION_TIME + 1
)
assert 'foo=bar' in html_text
36 changes: 36 additions & 0 deletions tests/notebooks/preheat/get_query_string.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "7efe3e8f-4b8d-4fd3-99d1-b06d79b88ae2",
"metadata": {},
"outputs": [],
"source": [
"from voila.utils import get_query_string\n",
"print(get_query_string())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 3 additions & 3 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ def start(self):
self.log.info('Storing connection files in %s.' % self.connection_dir)
self.log.info('Serving static files from %s.' % self.static_root)

# default server_url to base_url
self.server_url = self.server_url or self.base_url

self.kernel_spec_manager = KernelSpecManager(
parent=self
)
Expand Down Expand Up @@ -444,9 +447,6 @@ def start(self):
nbui = gettext.translation('nbui', localedir=os.path.join(ROOT, 'i18n'), fallback=True)
env.install_gettext_translations(nbui, newstyle=False)

# default server_url to base_url
self.server_url = self.server_url or self.base_url

self.app = tornado.web.Application(
base_url=self.base_url,
server_url=self.server_url or self.base_url,
Expand Down
1 change: 1 addition & 0 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def time_out():
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] = self.settings.get('server_url', '/')
kernel_id = await ensure_async(
(
self.kernel_manager.start_kernel(
Expand Down
7 changes: 5 additions & 2 deletions voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ENV_VARIABLE(str, Enum):
VOILA_PREHEAT = 'VOILA_PREHEAT'
VOILA_KERNEL_ID = 'VOILA_KERNEL_ID'
VOILA_BASE_URL = 'VOILA_BASE_URL'
VOILA_SERVER_URL = 'VOILA_SERVER_URL'
VOILA_APP_IP = 'VOILA_APP_IP'
VOILA_APP_PORT = 'VOILA_APP_PORT'
VOILA_WS_PROTOCOL = 'VOILA_WS_PROTOCOL'
Expand Down Expand Up @@ -89,8 +90,10 @@ def wait_for_request(url: str = None) -> str:
protocol = os.getenv(ENV_VARIABLE.VOILA_WS_PROTOCOL, 'ws')
server_ip = os.getenv(ENV_VARIABLE.VOILA_APP_IP, '127.0.0.1')
server_port = os.getenv(ENV_VARIABLE.VOILA_APP_PORT, '8866')
base_url = os.getenv(ENV_VARIABLE.VOILA_WS_BASE_URL, '/')
url = f'{protocol}://{server_ip}:{server_port}{base_url}voila/query'
server_url = os.getenv(ENV_VARIABLE.VOILA_SERVER_URL, '/')
# Use `VOILA_SERVER_URL` if `VOILA_WS_BASE_URL` not specified.
ws_base_url = os.getenv(ENV_VARIABLE.VOILA_WS_BASE_URL, server_url)
url = f'{protocol}://{server_ip}:{server_port}{ws_base_url}voila/query'

kernel_id = os.getenv(ENV_VARIABLE.VOILA_KERNEL_ID)
ws_url = f'{url}/{kernel_id}'
Expand Down
1 change: 1 addition & 0 deletions voila/voila_kernel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def fill_if_needed(
if key not in kernel_env:
kernel_env[key] = kernel_env_variables[key]
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_PREHEAT] = 'True'
kwargs['env'] = kernel_env

Expand Down