Skip to content

Commit 0a215bd

Browse files
committed
more settings example based on flags and file configs
1 parent 201bc07 commit 0a215bd

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

examples/simple/README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This folder contains an example of 2 simple extensions on top of Jupyter Server.
77
You need `python3` to build and run the server extensions.
88

99
```bash
10-
conda create -y -n jext python=3.7 && \
11-
conda activate jext && \
10+
conda create -y -n jupyter_server_example python=3.7 && \
11+
conda activate jupyter_server_example && \
1212
pip install -e .
1313
```
1414

@@ -33,10 +33,11 @@ pip uninstall -y simple_ext
3333
python setup.py install
3434
cp -r ./etc $(dirname $(which jupyter))/..
3535
# Start the jupyter server extension simple_ext1, it will also load simple_ext2 because of load_other_extensions = True..
36+
# When you invoke with the entrypoint, the default url will be opened in your browser.
3637
jupyter simple-ext1
3738
```
3839

39-
Now you can render Extension 1 Server content in your browser.
40+
Now you can render `Extension 1` Server content in your browser.
4041

4142
```bash
4243
# Jupyter Server Home Page.
@@ -60,7 +61,7 @@ open http://localhost:8888/simple_ext1/redirect
6061
open http://localhost:8888/static/simple_ext1/favicon.ico
6162
```
6263

63-
You can also render Extension 2 Server content in your browser.
64+
You can also render `Extension 2` Server content in your browser.
6465

6566
```bash
6667
# HTML static page.
@@ -71,7 +72,7 @@ open http://localhost:8888/simple_ext2/params/test?var1=foo
7172

7273
## Settings
7374

74-
Start with additional settings.
75+
Stop any running server (with CTRL+C) and start with additional settings on the command line.
7576

7677
```bash
7778
jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}" --SimpleApp1.cli=OK
@@ -83,12 +84,12 @@ Check the log, it should return on startup something like the following base on
8384
[SimpleApp1] SimpleApp1.app OK
8485
[SimpleApp1] SimpleApp1.file OK
8586
[SimpleApp1] SimpleApp1.cli OK
86-
[SimpleApp1] Complete Settings {'simple_ext1_config': {}, 'simple_ext1_template_paths': ['/home/datalayer/repos/jupyter-server/examples/simple/simple_ext1/templates'], 'simple_ext1_jinja2_env': <jinja2.environment.Environment object at 0x105ed7438>, 'log_function': <function log_request at 0x105e2d950>, 'base_url': '/', 'default_url': '/', 'template_path': ['/opt/datalayer/opt/miniconda3/envs/datalayer/lib/python3.7/site-packages/jupyter_server', '/opt/datalayer/opt/miniconda3/envs/datalayer/lib/python3.7/site-packages/jupyter_server/templates'], 'static_path': ['/opt/datalayer/opt/miniconda3/envs/datalayer/lib/python3.7/site-packages/jupyter_server/static'], 'static_custom_path': ['/home/datalayer/.jupyter/custom', '/opt/datalayer/opt/miniconda3/envs/datalayer/lib/python3.7/site-packages/jupyter_server/static/custom'], 'static_handler_class': <class 'jupyter_server.base.handlers.FileFindHandler'>, 'static_url_prefix': ...
87+
[SimpleApp1] Complete Settings {'simple_ext1_config': {}, 'simple_ext1_template_paths': ['/home/datalayer/repos/jupyter-server/examples/simple/simple_ext1/templates'], 'simple_ext1_jinja2_env': <jinja2.environment.Environment object at 0x105ed7438>, 'log_function': <function log_request at 0x105e2d950>, 'base_url': '/', 'default_url': '/', 'template_path': ...
8788
```
8889

8990
## Start only Extension 2
90-
`
91-
Now stop the server and start again with only Extension 2.
91+
92+
Now stop agin the server and start with only `Extension 2`.
9293

9394
```bash
9495
# Start the jupyter server extension simple_ext2, it will NOT load simple_ext1 because of load_other_extensions = False.
@@ -103,10 +104,14 @@ Try with the above links to check that only Extension 2 is responding (Extension
103104

104105
The `--hello` flag will log on startup `Hello Simple11 - You have provided the --hello flag or defined a c.SimpleApp1.hello == True`.
105106

107+
The `--simple11-dir` alias will set `SimpleExt11.simple11_dir` settings.
108+
109+
Stop any running server and then start the simple-ext11.
110+
106111
```bash
107-
jupyter simple-ext11 --hello
112+
jupyter simple-ext11 --hello --simple11-dir any_folder
108113
# or...
109-
jupyter server --ServerApp.jpserver_extensions="{'simple_ext11': True}" --hello
114+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext11': True}" --hello --simple11-dir any_folder
110115
```
111116

112117
Ensure the following URLs give respond correctly.

examples/simple/jupyter_server_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99

1010
c.SimpleApp1.file = 'OK'
1111
c.SimpleApp2.file = 'OK'
12+
13+
c.SimpleApp11.ignore_js = True

examples/simple/simple_ext1/application.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def initialize_settings(self):
5656
self.log.info('SimpleApp1.file {}'.format(self.get_conf('file')))
5757
self.log.info('SimpleApp1.cli {}'.format(self.get_conf('cli')))
5858
self.log.info('Complete Settings {}'.format(self.settings))
59+
# TODO Check this setting/config handling... Updating does not look to be fine here...
60+
self.settings["{}_config".format(self.extension_name)].update(**self.settings.get('config').get('SimpleApp1'))
5961

6062
#-----------------------------------------------------------------------------
6163
# Main entry point

examples/simple/simple_ext11/application.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import os
22
from simple_ext1.application import SimpleApp1
33
from jupyter_server.serverapp import aliases, flags
4+
from traitlets import Bool, Unicode, observe
45

56
DEFAULT_STATIC_FILES_PATH = os.path.join(os.path.dirname(__file__), "./../simple_ext1/static")
67
DEFAULT_TEMPLATE_FILES_PATH = os.path.join(os.path.dirname(__file__), "./../simple_ext1/templates")
78

89
class SimpleApp11(SimpleApp1):
910
flags['hello']=(
10-
{'SimpleApp11' : {'hello' : True}},
11-
"Say hello on startup."
11+
{ 'SimpleApp11' : {'hello' : False} }, "Say hello on startup."
1212
)
1313
aliases.update({
14-
'notebook-dir': 'ServerApp.notebook_dir',
14+
'simple11-dir': 'SimpleApp11.simple11_dir',
1515
})
1616

1717
# The name of the extension.
@@ -30,15 +30,43 @@ class SimpleApp11(SimpleApp1):
3030
DEFAULT_TEMPLATE_FILES_PATH
3131
]
3232

33+
simple11_dir = Unicode('',
34+
config=True,
35+
help='Simple directory'
36+
)
37+
38+
hello = Bool(False,
39+
config=True,
40+
help='Say hello',
41+
)
42+
43+
ignore_js = Bool(False,
44+
config=True,
45+
help='Ignore Javascript',
46+
)
47+
48+
@observe('ignore_js')
49+
def _update_ignore_js(self, change):
50+
"""TODO The observe does not work"""
51+
self.log.info('ignore_js has just changed: {}'.format(change))
52+
53+
@property
54+
def simple11_dir_formatted(self):
55+
return "/" + self.simple11_dir
56+
3357
def get_conf(self, key):
34-
simple_app_11 = self.settings.get('config').get('SimpleApp11')
35-
if simple_app_11:
36-
return simple_app_11.get(key, None)
37-
return None
58+
return self.settings.get('config').get('SimpleApp11').get(key, None)
3859

3960
def initialize_settings(self):
61+
self.log.info('SimpleApp11.hello: {}'.format(self.get_conf('hello')))
62+
self.log.info('hello: {}'.format(self.hello))
4063
if self.get_conf('hello') == True:
41-
self.log.info('Hello Simple11 - You have provided the --hello flag or defined a c.SimpleApp1.hello == True')
64+
self.log.info("Hello Simple11 - You have provided the --hello flag or defined 'c.SimpleApp1.hello == True' in jupyter_server_config.py")
65+
self.log.info('SimpleApp11.simple11_dir: {}'.format(self.get_conf('simple11_dir')))
66+
self.log.info('SimpleApp11.ignore_js: {}'.format(self.get_conf('ignore_js')))
67+
self.log.info('ignore_js: {}'.format(self.ignore_js))
68+
# TODO Check this setting/config handling... Updating does not look to be fine here...
69+
self.settings["{}_config".format(self.extension_name)].update(**self.settings.get('config').get('SimpleApp11'))
4270
super().initialize_settings()
4371

4472
#-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)