Skip to content

Commit 43b7447

Browse files
committed
rename configs
1 parent f15e523 commit 43b7447

File tree

8 files changed

+58
-104
lines changed

8 files changed

+58
-104
lines changed

examples/simple/README.md

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

99
```bash
10+
# Clone, create a conda env and install from source.
1011
git clone https://github.com/jupyter/jupyter_server && \
1112
cd examples/simple && \
1213
conda create -y -n jupyter_server_example python=3.7 && \
1314
conda activate jupyter_server_example && \
1415
pip install -e .
1516
```
1617

17-
**OPTIONAL** If you want to build the Typescript code, you need `npm` on your local env. Compiled javascript is provided as artifact in this repository, so this Typescript build step is optional. The Typescript source and configuration has been taken from https://github.com/markellekelly/jupyter-server-example.
18+
**OPTIONAL** If you want to build the Typescript code, you need [npm](https://www.npmjs.com) on your local environement. Compiled javascript is provided as artifact in this repository, so this Typescript build step is optional. The Typescript source and configuration have been taken from https://github.com/markellekelly/jupyter-server-example.
1819

1920
```bash
2021
npm install && \
2122
npm run build
2223
```
2324

24-
## Start Extension 1
25+
## No Extension
26+
27+
Ensure Jupyter Server is starting without any extension enabled.
28+
29+
```bash
30+
# Run this command from a shell.
31+
jupyter server
32+
```
33+
34+
Browse the default home page, it should show a white page in your browser with the following content: `A Jupyter Server is running.`
35+
36+
```bash
37+
# Jupyter Server default Home Page.
38+
open http://localhost:8888
39+
```
40+
41+
## Extension 1
2542

2643
```bash
2744
# Start the jupyter server activating simple_ext1 extension.
@@ -31,8 +48,6 @@ jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True}"
3148
Now you can render `Extension 1` Server content in your browser.
3249

3350
```bash
34-
# Jupyter Server Home Page.
35-
open http://localhost:8888/
3651
# Home page as defined by default_url = '/default'.
3752
open http://localhost:8888/simple_ext1/default
3853
# HTML static page.
@@ -52,16 +67,16 @@ open http://localhost:8888/simple_ext1/redirect
5267
open http://localhost:8888/static/simple_ext1/favicon.ico
5368
```
5469

55-
## Start Extension 2
70+
## Extension 1 and Extension 2
5671

57-
The following command starts both `simple_ext1` and `simple_ext2` extensions.
72+
The following command starts both the `simple_ext1` and `simple_ext2` extensions.
5873

5974
```bash
6075
# Start the jupyter server, it will load both simple_ext1 and simple_ext2 based on the provided trait.
6176
jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}"
6277
```
6378

64-
You can also render `Extension 2` Server content in your browser.
79+
Check that the previous `Extension 1` content is still available ant that you can also render `Extension 2` Server content in your browser.
6580

6681
```bash
6782
# HTML static page.
@@ -70,7 +85,7 @@ open http://localhost:8888/static/simple_ext2/test.html
7085
open http://localhost:8888/simple_ext2/params/test?var1=foo
7186
```
7287

73-
## Start with Entrypoints
88+
## Work with Entrypoints
7489

7590
Optionally, you can copy `simple_ext1.json` and `simple_ext2.json` configuration to your env `etc` folder and start only Extension 1, which will also start Extension 2.
7691

@@ -90,20 +105,20 @@ Stop any running server (with `CTRL+C`) and start with additional configuration
90105
The provided settings via CLI will override the configuration that reside in the files (`jupyter_simple_ext1_config.py`...)
91106

92107
```bash
93-
jupyter server \
94-
--ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}" \
95-
--SimpleApp1.cli=OK
108+
jupyter simple-ext1 --SimpleApp1.configA "ConfigA from command line"
96109
```
97110

98-
Check the log, it should return on startup something like the following base on the trait you have defined in the CLI and in the `jupyter_server_config.py`.
111+
Check the log, it should return on startup something like the following base on the trait you have defined in the CLI and in the `jupyter_simple_ext1_config.py`.
99112

100113
```
101-
[SimpleApp1] SimpleApp1.app OK
102-
[SimpleApp1] SimpleApp1.file OK
103-
[SimpleApp1] SimpleApp1.cli OK
114+
[SimpleApp1] Config {'SimpleApp1': {'configA': 'ConfigA from file', 'configB': 'ConfigB from file', 'configC': 'ConfigC from file'}}
115+
[SimpleApp1] Config {'SimpleApp1': {'configA': 'ConfigA from file', 'configB': 'ConfigB from file', 'configC': 'ConfigC from file'}}
116+
[SimpleApp2] WARNING | Config option `configD` not recognized by `SimpleApp2`. Did you mean `config_file`?
117+
[SimpleApp2] Config {'SimpleApp2': {'configD': 'ConfigD from file'}}
118+
[SimpleApp1] Config {'SimpleApp1': {'configA': 'ConfigA from command line', 'configB': 'ConfigB from file', 'configC': 'ConfigC from file'}}
104119
```
105120

106-
## Start only Extension 2
121+
## Only Extension 2
107122

108123
Now stop agin the server and start with only `Extension 2`.
109124

@@ -114,43 +129,18 @@ jupyter simple-ext2
114129

115130
Try with the above links to check that only Extension 2 is responding (Extension 1 URLs should give you an 404 error).
116131

117-
## Extension 11 extending Extension 1
132+
## Extension 11 extends Extension 1
118133

119134
`Extension 11` extends `Extension 1` and brings a few more configs.
120135

121136
Run `jupyter simple-ext11 --generate-config && vi ~/.jupyter/jupyter_config.py`.
122137

138+
> --generate-config returns an execption `"The ExtensionApp has not ServerApp "`
139+
123140
The generated configuration should contains the following.
124141

125142
```bash
126-
...
127-
# Can be used to override templates from notebook.templates.
128-
#c.ExtensionApp.template_paths = []
129-
130-
#------------------------------------------------------------------------------
131-
# SimpleApp1(ExtensionApp) configuration
132-
#------------------------------------------------------------------------------
133-
134-
#------------------------------------------------------------------------------
135-
# SimpleApp11(SimpleApp1) configuration
136-
#------------------------------------------------------------------------------
137-
138-
## Say hello
139-
#c.SimpleApp11.hello = False
140-
141-
## Ignore Javascript
142-
#c.SimpleApp11.ignore_js = False
143-
144-
## Simple directory
145-
#c.SimpleApp11.simple11_dir = ''
146-
147-
#------------------------------------------------------------------------------
148-
# ServerApp(JupyterApp) configuration
149-
#------------------------------------------------------------------------------
150-
151-
## Set the Access-Control-Allow-Credentials: true header
152-
#c.ServerApp.allow_credentials = False
153-
...
143+
TBD
154144
```
155145

156146
The `hello`, `ignore_js` and `simple11_dir` are traits defined on the SimpleApp11 class.

examples/simple/jupyter_server_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
#------------------------------------------------------------------------------
66

77
## The date format used by logging formatters for %(asctime)s
8-
c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S Simple_Extensions'
8+
c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S Simple_Extensions_Example'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
c.SimpleApp1.app = 'an app'
2-
c.SimpleApp1.cli = 'a cli'
3-
c.SimpleApp1.file = 'a file'
1+
c.SimpleApp1.configA = 'ConfigA from file'
2+
c.SimpleApp1.configB = 'ConfigB from file'
3+
c.SimpleApp1.configC = 'ConfigC from file'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c.SimpleApp2.file = 'OK'
1+
c.SimpleApp2.configD = 'ConfigD from file'

examples/simple/simple_ext1/application.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ class SimpleApp1(ExtensionApp):
2828
DEFAULT_TEMPLATE_FILES_PATH
2929
]
3030

31-
file = Unicode('',
31+
configA = Unicode('',
3232
config=True,
33-
help='File path'
33+
help='Config A example.'
3434
)
3535

36-
app = Unicode('',
36+
configB = Unicode('',
3737
config=True,
38-
help='File path'
38+
help='Config B example.'
3939
)
4040

41-
cli = Unicode('',
41+
configC = Unicode('',
4242
config=True,
43-
help='File path'
43+
help='Config C example.'
4444
)
4545

4646
def initialize_handlers(self):
@@ -53,31 +53,8 @@ def initialize_handlers(self):
5353
(r'/{}/(.*)', ErrorHandler)
5454
])
5555

56-
def initialize_templates(self):
57-
jenv_opt = {"autoescape": True}
58-
env = jinja2.Environment(
59-
loader=jinja2.FileSystemLoader(self.template_paths),
60-
extensions=["jinja2.ext.i18n"],
61-
**jenv_opt
62-
)
63-
template_settings = {"simple_ext1_jinja2_env": env}
64-
self.settings.update(**template_settings)
65-
66-
def get_conf(self, key):
67-
c = self.settings.get('config').get(type(self).__name__)
68-
if c:
69-
return c.get(key, None)
70-
else:
71-
return None
72-
7356
def initialize_settings(self):
7457
self.log.info('Config {}'.format(self.config))
75-
# self.log.info('SimpleApp1.app {}'.format(self.get_conf('app')))
76-
# self.log.info('SimpleApp1.file {}'.format(self.get_conf('file')))
77-
# self.log.info('SimpleApp1.cli {}'.format(self.get_conf('cli')))
78-
# self.log.info('Complete Settings {}'.format(self.settings))
79-
# self.settings.get('config').get(self.extension_name).update({'app': 'OK'})
80-
# self.settings["{}_config".format(self.extension_name)].update(**self.settings.get('config').get('SimpleApp1'))
8158

8259
#-----------------------------------------------------------------------------
8360
# Main entry point

examples/simple/simple_ext1/handlers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
class DefaultHandler(ExtensionHandler):
44
def get(self):
5-
# The ExtensionApp’s config object.
6-
# self.log.info("Settings in {} Default Handler: {}".format(self.extension_name, self.settings))
7-
self.log.info("Config in {} Default Handler: {}".format(self.extension_name, self.config))
8-
# self.log.info(self.server_config)
95
# The name of the extension to which this handler is linked.
106
self.log.info("Extension Name in {} Default Handler: {}".format(self.extension_name, self.extension_name))
117
# A method for getting the url to static files (prefixed with /static/<extension_name>).
128
self.log.info("Static URL for / in simple_ext1 Default Handler:".format(self.static_url(path='/')))
139
self.write('<h1>Hello Simple 1 - I am the default...</h1>')
10+
self.write('Config in {} Default Handler: {}'.format(self.extension_name, self.config))
1411

1512
class RedirectHandler(ExtensionHandler):
1613
def get(self):
@@ -28,15 +25,15 @@ def get(self, matched_part=None, *args, **kwargs):
2825
class BaseTemplateHandler(ExtensionHandler):
2926
def get_template(self, path):
3027
"""Return the jinja template object for a given name"""
31-
return self.settings['simple_ext1_jinja2_env'].get_template(path)
28+
return self.settings['{}_jinja2_env'.format(self.extension_name)].get_template(path)
3229

3330
class TypescriptHandler(BaseTemplateHandler):
3431
def get(self):
3532
self.write(self.render_template("typescript.html"))
3633

3734
class TemplateHandler(BaseTemplateHandler):
3835
def get(self, path):
39-
# print(self.get_template('simple1.html'))
36+
""" Optionaly, you can print(self.get_template('simple1.html'))"""
4037
self.write(self.render_template('simple1.html', path=path))
4138

4239
class ErrorHandler(BaseTemplateHandler):

examples/simple/simple_ext11/application.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,11 @@ def simple11_dir_formatted(self):
5555
return "/" + self.simple11_dir
5656

5757
def initialize_settings(self):
58-
self.log.info('SimpleApp11.hello: {}'.format(super().get_conf('hello')))
5958
self.log.info('hello: {}'.format(self.hello))
60-
if self.get_conf('hello') == True:
59+
if self.config['hello'] == True:
6160
self.log.info("Hello Simple11 - You have provided the --hello flag or defined 'c.SimpleApp1.hello == True' in jupyter_server_config.py")
62-
self.log.info('SimpleApp11.simple11_dir: {}'.format(self.get_conf('simple11_dir')))
63-
self.log.info('SimpleApp11.ignore_js: {}'.format(self.get_conf('ignore_js')))
6461
self.log.info('ignore_js: {}'.format(self.ignore_js))
65-
c = self.settings.get('config').get(type(self).__name__)
66-
if c:
67-
self.settings["{}_config".format(self.extension_name)].update(**c)
68-
# super().initialize_settings()
62+
super().initialize_settings()
6963

7064
#-----------------------------------------------------------------------------
7165
# Main entry point

examples/simple/simple_ext2/application.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os, jinja2
2+
from traitlets import Unicode
23
from jupyter_server.extension.application import ExtensionApp
34
from .handlers import ParameterHandler, TemplateHandler, IndexHandler, ErrorHandler
45

@@ -26,6 +27,11 @@ class SimpleApp2(ExtensionApp):
2627
DEFAULT_TEMPLATE_FILES_PATH
2728
]
2829

30+
configD = Unicode('',
31+
config=True,
32+
help='Config D example.'
33+
)
34+
2935
def initialize_handlers(self):
3036
self.handlers.extend([
3137
(r'/simple_ext2/params/(.+)$', ParameterHandler),
@@ -34,18 +40,8 @@ def initialize_handlers(self):
3440
(r'/simple_ext2/(.*)', ErrorHandler)
3541
])
3642

37-
def initialize_templates(self):
38-
jenv_opt = {"autoescape": True}
39-
env = jinja2.Environment(
40-
loader=jinja2.FileSystemLoader(self.template_paths),
41-
extensions=["jinja2.ext.i18n"],
42-
**jenv_opt
43-
)
44-
template_settings = {"simple_ext2_jinja2_env": env}
45-
self.settings.update(**template_settings)
46-
4743
def initialize_settings(self):
48-
pass
44+
self.log.info('Config {}'.format(self.config))
4945

5046
#-----------------------------------------------------------------------------
5147
# Main entry point

0 commit comments

Comments
 (0)