Skip to content

Commit ea0555a

Browse files
committed
more examples related to settings and extension
1 parent 58eb525 commit ea0555a

File tree

10 files changed

+154
-40
lines changed

10 files changed

+154
-40
lines changed

examples/simple/README.md

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ 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
12-
pip install -e .
10+
conda create -y -n jext python=3.7 && \
11+
conda activate jext && \
12+
pip install -e .
1313
```
1414

1515
**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.
@@ -36,21 +36,14 @@ cp -r ./etc $(dirname $(which jupyter))/..
3636
jupyter simple-ext1
3737
```
3838

39-
Now you can render Server content in your browser.
39+
Now you can render Extension 1 Server content in your browser.
4040

4141
```bash
42-
# Default server page should redirect to `/static/simple_ext1/favicon.ico`.
43-
open http://localhost:8888
44-
```
45-
46-
Render Extension 1 Server content in your browser.
47-
x
48-
```bash
49-
# Jupyter Server Home Page
42+
# Jupyter Server Home Page.
5043
open http://localhost:8888/
51-
# TODO Fix Default URL.
52-
# Home page as defined by default_url = '/template1/home'.
53-
open http://localhost:8888/simple_ext1/
44+
# TODO Fix Default URL, it does not show on startup.
45+
# Home page as defined by default_url = '/default'.
46+
open http://localhost:8888/simple_ext1/default
5447
# HTML static page.
5548
open http://localhost:8888/static/simple_ext1/test.html
5649
# Content from Handlers.
@@ -67,7 +60,7 @@ open http://localhost:8888/simple_ext1/redirect
6760
open http://localhost:8888/static/simple_ext1/favicon.ico
6861
```
6962

70-
Render Extension 2 Server content in your browser.
63+
You can also render Extension 2 Server content in your browser.
7164

7265
```bash
7366
# HTML static page.
@@ -76,8 +69,25 @@ open http://localhost:8888/static/simple_ext2/test.html
7669
open http://localhost:8888/simple_ext2/params/test?var1=foo
7770
```
7871

79-
## Start only Extension 2
72+
## Settings
73+
74+
Start with additional settings.
75+
76+
```bash
77+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}" --SimpleApp1.cli=OK
78+
```
79+
80+
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`.
8081

82+
```
83+
[SimpleApp1] SimpleApp1.app OK
84+
[SimpleApp1] SimpleApp1.file OK
85+
[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+
```
88+
89+
## Start only Extension 2
90+
`
8191
Now stop the server and start again with only Extension 2.
8292

8393
```bash
@@ -86,3 +96,33 @@ jupyter simple-ext2
8696
```
8797

8898
Try with the above links to check that only Extension 2 is responding (Extension 1 URLs should give you an 404 error).
99+
100+
## Extension 11 extending Extension 1
101+
102+
```bash
103+
jupyter simple-ext11
104+
# or...
105+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext11': True}"
106+
```
107+
108+
```bash
109+
# Jupyter Server Home Page.
110+
open http://localhost:8888/
111+
# TODO Fix Default URL, it does not show on startup.
112+
# Home page as defined by default_url = '/default'.
113+
open http://localhost:8888/simple_ext11/default
114+
# HTML static page.
115+
open http://localhost:8888/static/simple_ext11/test.html
116+
# Content from Handlers.
117+
open http://localhost:8888/simple_ext11/params/test?var1=foo
118+
# Content from Template.
119+
open http://localhost:8888/simple_ext11/template1/test
120+
# Content from Template with Typescript.
121+
open http://localhost:8888/simple_ext11/typescript
122+
# Error content.
123+
open http://localhost:8888/simple_ext11/nope
124+
# Redirect.
125+
open http://localhost:8888/simple_ext11/redirect
126+
# Favicon static content.
127+
open http://localhost:8888/static/simple_ext11/favicon.ico
128+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"ServerApp": {
3+
"jpserver_extensions": {
4+
"simple_ext11": true
5+
}
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Configuration file for jupyter-server extensions.
2+
3+
#------------------------------------------------------------------------------
4+
# Application(SingletonConfigurable) configuration
5+
#------------------------------------------------------------------------------
6+
7+
## The date format used by logging formatters for %(asctime)s
8+
c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S Simple_Extensions'
9+
10+
c.SimpleApp1.file = 'OK'
11+
c.SimpleApp2.file = 'OK'

examples/simple/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def add_data_files(path):
1313
for (dirpath, dirnames, filenames) in os.walk(path):
1414
if filenames:
1515
data_files.append((dirpath, [os.path.join(dirpath, filename) for filename in filenames]))
16-
# Add all the templates
16+
# Add all static and templates folders.
1717
add_data_files('simple_ext1/static')
1818
add_data_files('simple_ext1/templates')
1919
add_data_files('simple_ext2/static')
@@ -40,6 +40,7 @@ def add_data_files(path):
4040
entry_points = {
4141
'console_scripts': [
4242
'jupyter-simple-ext1 = simple_ext1.application:main',
43+
'jupyter-simple-ext11 = simple_ext11.application:main',
4344
'jupyter-simple-ext2 = simple_ext2.application:main'
4445
]
4546
},

examples/simple/simple_ext1/application.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import os, jinja2
22
from jupyter_server.extension.application import ExtensionApp
3-
from .handlers import RedirectHandler, ParameterHandler, TemplateHandler, TypescriptHandler, ErrorHandler
3+
from .handlers import (DefaultHandler, RedirectHandler, ParameterHandler,
4+
TemplateHandler, TypescriptHandler, ErrorHandler)
45

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

89
class SimpleApp1(ExtensionApp):
910

10-
# The name of the extension
11+
# The name of the extension.
1112
extension_name = "simple_ext1"
1213

1314
# Te url that your extension will serve its homepage.
14-
default_url = '/template/home'
15+
default_url = '/default'
1516

1617
# Should your extension expose other server extensions when launched directly?
1718
load_other_extensions = True
@@ -28,11 +29,12 @@ class SimpleApp1(ExtensionApp):
2829

2930
def initialize_handlers(self):
3031
self.handlers.extend([
31-
(r'/simple_ext1/params/(.+)$', ParameterHandler),
32-
(r'/simple_ext1/template1/(.*)$', TemplateHandler),
33-
(r'/simple_ext1/redirect', RedirectHandler),
34-
(r'/simple_ext1/typescript/?', TypescriptHandler),
35-
(r'/simple_ext1/(.*)', ErrorHandler)
32+
(r'/{}/default'.format(self.extension_name), DefaultHandler),
33+
(r'/{}/params/(.+)$'.format(self.extension_name), ParameterHandler),
34+
(r'/{}/template1/(.*)$'.format(self.extension_name), TemplateHandler),
35+
(r'/{}/redirect'.format(self.extension_name), RedirectHandler),
36+
(r'/{}/typescript/?'.format(self.extension_name), TypescriptHandler),
37+
(r'/{}/(.*)', ErrorHandler)
3638
])
3739

3840
def initialize_templates(self):
@@ -45,8 +47,15 @@ def initialize_templates(self):
4547
template_settings = {"simple_ext1_jinja2_env": env}
4648
self.settings.update(**template_settings)
4749

50+
def get_conf(self, key):
51+
return self.settings.get('config').get('SimpleApp1').get(key, None)
52+
4853
def initialize_settings(self):
49-
pass
54+
self.settings.get('config').get('SimpleApp1').update({'app': 'OK'})
55+
self.log.info('SimpleApp1.app {}'.format(self.get_conf('app')))
56+
self.log.info('SimpleApp1.file {}'.format(self.get_conf('file')))
57+
self.log.info('SimpleApp1.cli {}'.format(self.get_conf('cli')))
58+
self.log.info('Complete Settings {}'.format(self.settings))
5059

5160
#-----------------------------------------------------------------------------
5261
# Main entry point
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
from jupyter_server.extension.handler import ExtensionHandler
22

3+
class DefaultHandler(ExtensionHandler):
4+
def get(self):
5+
# The ExtensionApp’s config object.
6+
self.log.info("Settings: {}".format(self.settings))
7+
self.log.info("Config: {}".format(self.config))
8+
# TODO The ServerApp’s config object.
9+
# self.log.info(self.server_config)
10+
# The name of the extension to which this handler is linked.
11+
self.log.info("Extension Name: {}".format(self.extension_name))
12+
# A method for getting the url to static files (prefixed with /static/<extension_name>).
13+
self.log.info("Static URL for /:".format(self.static_url(path='/')))
14+
self.write('<h1>Hello Simple 1 - I am the default...</h1>')
15+
316
class RedirectHandler(ExtensionHandler):
417
def get(self):
5-
self.redirect("/static/simple_ext1/favicon.ico")
18+
self.redirect("/static/{}/favicon.ico".format(self.extension_name))
619

7-
class ParameterHandler(ExtensionHandler):
8-
20+
class ParameterHandler(ExtensionHandler):
921
def get(self, matched_part=None, *args, **kwargs):
1022
var1 = self.get_argument('var1', default=None)
1123
components = [x for x in self.request.path.split("/") if x]
@@ -15,23 +27,19 @@ def get(self, matched_part=None, *args, **kwargs):
1527
self.write('<p>components: {}</p>'.format(components))
1628

1729
class BaseTemplateHandler(ExtensionHandler):
18-
1930
def get_template(self, path):
2031
"""Return the jinja template object for a given name"""
2132
return self.settings['simple_ext1_jinja2_env'].get_template(path)
2233

2334
class TypescriptHandler(BaseTemplateHandler):
24-
2535
def get(self):
2636
self.write(self.render_template("typescript.html"))
2737

2838
class TemplateHandler(BaseTemplateHandler):
29-
3039
def get(self, path):
3140
# print(self.get_template('simple1.html'))
3241
self.write(self.render_template('simple1.html', path=path))
3342

3443
class ErrorHandler(BaseTemplateHandler):
35-
3644
def get(self, path):
3745
self.write(self.render_template('error.html', path=path))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .application import SimpleApp1
2+
3+
def _jupyter_server_extension_paths():
4+
return [
5+
{'module': 'simple_ext1'}
6+
]
7+
8+
load_jupyter_server_extension = SimpleApp1.load_jupyter_server_extension
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from simple_ext1.application import SimpleApp1
3+
4+
DEFAULT_STATIC_FILES_PATH = os.path.join(os.path.dirname(__file__), "./../simple_ext1/static")
5+
DEFAULT_TEMPLATE_FILES_PATH = os.path.join(os.path.dirname(__file__), "./../simple_ext1/templates")
6+
7+
class SimpleApp11(SimpleApp1):
8+
9+
# The name of the extension.
10+
extension_name = "simple_ext11"
11+
12+
# Local path to static files directory.
13+
static_paths = [
14+
DEFAULT_STATIC_FILES_PATH
15+
]
16+
17+
# Local path to templates directory.
18+
template_paths = [
19+
DEFAULT_TEMPLATE_FILES_PATH
20+
]
21+
22+
def initialize_handlers(self):
23+
super().initialize_handlers()
24+
25+
def initialize_templates(self):
26+
super().initialize_templates()
27+
28+
def initialize_settings(self):
29+
super().initialize_templates()
30+
31+
#-----------------------------------------------------------------------------
32+
# Main entry point
33+
#-----------------------------------------------------------------------------
34+
35+
main = launch_new_instance = SimpleApp11.launch_instance

examples/simple/simple_ext2/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class SimpleApp2(ExtensionApp):
99

10-
# The name of the extension
10+
# The name of the extension.
1111
extension_name = "simple_ext2"
1212

1313
# Te url that your extension will serve its homepage.
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from jupyter_server.extension.handler import ExtensionHandler
22

33
class ParameterHandler(ExtensionHandler):
4-
54
def get(self, matched_part=None, *args, **kwargs):
65
var1 = self.get_argument('var1', default=None)
76
components = [x for x in self.request.path.split("/") if x]
@@ -11,23 +10,19 @@ def get(self, matched_part=None, *args, **kwargs):
1110
self.write('<p>components: {}</p>'.format(components))
1211

1312
class BaseTemplateHandler(ExtensionHandler):
14-
1513
def get_template(self, name):
1614
"""Return the jinja template object for a given name"""
1715
return self.settings['simple_ext2_jinja2_env'].get_template(name)
1816

1917
class IndexHandler(BaseTemplateHandler):
20-
2118
def get(self):
2219
self.write(self.render_template("index.html"))
2320

2421
class TemplateHandler(BaseTemplateHandler):
25-
2622
def get(self, path):
2723
print(self.get_template('simple_ext2.html'))
2824
self.write(self.render_template('simple_ext2.html', path=path))
2925

3026
class ErrorHandler(BaseTemplateHandler):
31-
3227
def get(self, path):
3328
self.write(self.render_template('error.html'))

0 commit comments

Comments
 (0)