Skip to content

Commit acdd9c1

Browse files
dcnadlerdcnadlertrungleduc
authored
Add Voila configuration option for default kernel environmental variables (#1175)
* Adds default kernel env varaibles to configuration options * Adds test for default kernel env variables * Docs update for default kernal env variables CLI option * Move default_env_variables to ViolaKernelManager * Update documentation Co-authored-by: dcnadler <[email protected]> Co-authored-by: Duc Trung LE <[email protected]>
1 parent 211ca29 commit acdd9c1

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

docs/source/customize.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,14 @@ Preheated kernels
424424
==================
425425

426426
Since Voilà needs to start a new jupyter kernel and execute the requested notebook in this kernel for every connection, this would lead to a long waiting time before the widgets can be displayed in the browser.
427-
To reduce this waiting time, especially for the heavy notebooks, users can activate the preheating kernel option of Voilà.
427+
To reduce this waiting time, especially for heavy notebooks, users can activate the preheating kernel option of Voilà.
428428

429429
.. warning::
430430
Because preheated kernels are not executed on request, this feature is incompatible with the `prelaunch-hook` functionality.
431431

432432
This option will enable two features:
433433

434+
434435
- A pool of kernels is started for each notebook and kept in standby, then the notebook is executed in every kernel of its pool. When a new client requests a kernel, the preheated kernel in this pool is used and another kernel is started asynchronously to refill the pool.
435436
- The HTML version of the notebook is rendered in each preheated kernel and stored, when a client connects to Voila, under some conditions, the cached HTML is served instead of re-rendering the notebook.
436437

@@ -440,7 +441,15 @@ The preheating kernel option works with any kernel manager, it is deactivated by
440441
441442
voila --preheat_kernel=True --pool_size=5
442443
443-
If the pool size does not match the user's requirements, or some notebooks need to use environment variables..., additional settings are needed. The easiest way to change these settings is to provide a file named `voila.json` in the same folder containing the notebooks. Settings for preheating kernel ( list of notebooks does not need preheated kernels, number of kernels in pool, refilling delay, environment variables for starting kernel...) can be set under the `VoilaKernelManager` class name.
444+
The default environment variables for preheated kernels can be set by the `VoilaKernelManager.default_env_variables` setting. For example, this command
445+
446+
.. code-block:: bash
447+
448+
voila --preheat_kernel=True --VoilaKernelManager.default_env_variables='{"FOO": "BAR"}'
449+
450+
will set the variable "FOO" in all preheated kernels.
451+
452+
If the pool size does not match the user's requirements, or some notebooks need to use specific environment variables..., additional settings are needed. The easiest way to change these settings is to provide a file named `voila.json` in the same folder containing the notebooks. Settings for preheating kernel ( list of notebooks does not need preheated kernels, number of kernels in pool, refilling delay, environment variables for starting kernel...) can be set under the `VoilaKernelManager` class name.
444453

445454
Here is an example of settings with explanations for preheating kernel option.
446455

@@ -519,7 +528,7 @@ In preheating kernel mode, users can prepend with ``wait_for_request`` from ``vo
519528
520529
``wait_for_request`` will pause the execution of the notebook in the preheated kernel at this cell and wait for an actual user to connect to Voilà, set the request info environment variables and then continue the execution of the remaining cells.
521530

522-
If the Voilà websocket handler is not started with the default protocol (`ws`), the default IP address (`127.0.0.1`) the default port (`8866`) or with url suffix, users need to provide these values through the environment variables ``VOILA_WS_PROTOCOL``, ``VOILA_APP_IP``, ``VOILA_APP_PORT`` and ``VOILA_WS_BASE_URL``. The easiest way is to set these variables in the `voila.json` configuration file, for example:
531+
If the Voilà websocket handler is not started with the default protocol (`ws`), the default IP address (`127.0.0.1`) the default port (`8866`) or with url suffix, users need to provide these values through the environment variables ``VOILA_WS_PROTOCOL``, ``VOILA_APP_IP``, ``VOILA_APP_PORT`` and ``VOILA_WS_BASE_URL``. One way to set these variables is in the `voila.json` configuration file, for example:
523532

524533
.. code-block:: python
525534
@@ -540,6 +549,12 @@ If the Voilà websocket handler is not started with the default protocol (`ws`),
540549
}
541550
}
542551
552+
Additionally, you can set these with the command:
553+
554+
.. code-block:: bash
555+
556+
voila --preheat_kernel=True --VoilaKernelManager.default_env_variables='{"VOILA_WS_PROTOCOL":"wss","VOILA_APP_IP":"192.168.1.1"}'
557+
543558
Hiding output and code cells based on cell tags
544559
===============================================
545560

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import asyncio
2+
import os
3+
4+
import pytest
5+
6+
7+
@pytest.fixture()
8+
def voila_args_extra():
9+
return ['--VoilaKernelManager.default_env_variables={"FOO": "BAR"}']
10+
11+
12+
@pytest.fixture
13+
def preheat_mode():
14+
return True
15+
16+
17+
@pytest.fixture
18+
def voila_notebook(notebook_directory):
19+
return os.path.join(notebook_directory, 'preheat', 'default_env_variables.ipynb')
20+
21+
22+
NOTEBOOK_EXECUTION_TIME = 2
23+
24+
25+
async def send_request(sc, url, wait=0):
26+
await asyncio.sleep(wait)
27+
response = await sc.fetch(url)
28+
return response.body.decode('utf-8')
29+
30+
31+
async def test_default_kernel_env_variable(http_server_client, base_url):
32+
html_text = await send_request(sc=http_server_client,
33+
url=base_url,
34+
wait=NOTEBOOK_EXECUTION_TIME + 1)
35+
assert 'BAR' in html_text
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "7efe3e8f-4b8d-4fd3-99d1-b06d79b88ae2",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import time\n",
11+
"time.sleep(2)\n",
12+
"print('hello world')"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "782377cd-2bc0-4d0d-8b63-74dcb7e9d645",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"import os\n",
23+
"os.getenv('FOO')"
24+
]
25+
}
26+
],
27+
"metadata": {
28+
"kernelspec": {
29+
"display_name": "Python 3.10.5 ('voila')",
30+
"language": "python",
31+
"name": "python3"
32+
}
33+
},
34+
"nbformat": 4,
35+
"nbformat_minor": 5
36+
}

voila/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def start(self):
457457
kernel_manager_class = voila_kernel_manager_factory(
458458
self.voila_configuration.multi_kernel_manager_class,
459459
preheat_kernel,
460-
pool_size
460+
pool_size,
461461
)
462462
self.kernel_manager = kernel_manager_class(
463463
parent=self,

voila/voila_kernel_manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def voila_kernel_manager_factory(base_class: Type[T], preheat_kernel: bool, defa
4141
Args:
4242
- base_class (Type[T]): The kernel manager class
4343
- preheat_kernel (Bool): Flag to decorate the input class
44+
- default_pool_size (int): Size of pre-heated kernel pool for each notebook.
45+
Zero or negative number means disabled
4446
4547
Returns:
4648
T: Decorated class
@@ -73,6 +75,13 @@ class VoilaKernelManager(base_class):
7375
variables used to start kernel''',
7476
)
7577

78+
default_env_variables = Dict(
79+
{},
80+
config=True,
81+
help='''Default environmental variables for kernels
82+
''',
83+
)
84+
7685
preheat_blacklist = List([],
7786
config=True,
7887
help='List of notebooks which do not use pre-heated kernel.')
@@ -85,7 +94,12 @@ class VoilaKernelManager(base_class):
8594

8695
@default('kernel_pools_config')
8796
def _kernel_pools_config(self):
88-
return {'default': {'pool_size': max(default_pool_size, 0), 'kernel_env_variables': {}}}
97+
return {
98+
'default': {
99+
'pool_size': max(default_pool_size, 0),
100+
'kernel_env_variables': self.default_env_variables,
101+
}
102+
}
89103

90104
def __init__(self, **kwargs):
91105

0 commit comments

Comments
 (0)