Skip to content

Commit 33cf8e6

Browse files
authored
Merge pull request #117 from datalayer-contrib/example
simple extension example
2 parents 01031c6 + c3a1940 commit 33cf8e6

39 files changed

+935
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ config.rst
4242

4343
package-lock.json
4444
geckodriver.log
45+
yarn.lock

examples/simple/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Jupyter Server Simple Extension Example
2+
3+
This folder contains example of simple extensions on top of Jupyter Server and review configuration aspects.
4+
5+
## Install
6+
7+
You need `python3` to build and run the server extensions.
8+
9+
```bash
10+
# Clone, create a conda env and install from source.
11+
git clone https://github.com/jupyter/jupyter_server && \
12+
cd examples/simple && \
13+
conda create -y -n jupyter-server-example python=3.7 && \
14+
conda activate jupyter-server-example && \
15+
pip install -e .
16+
```
17+
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.
19+
20+
```bash
21+
npm install && \
22+
npm run build
23+
```
24+
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
42+
43+
```bash
44+
# Start the jupyter server activating simple_ext1 extension.
45+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True}"
46+
```
47+
48+
Now you can render `Extension 1` Server content in your browser.
49+
50+
```bash
51+
# Home page as defined by default_url = '/default'.
52+
open http://localhost:8888/simple_ext1/default
53+
# HTML static page.
54+
open http://localhost:8888/static/simple_ext1/home.html
55+
open http://localhost:8888/static/simple_ext1/test.html
56+
# Content from Handlers.
57+
open http://localhost:8888/simple_ext1/params/test?var1=foo
58+
# Content from Template.
59+
open http://localhost:8888/simple_ext1/template1/test
60+
# Content from Template with Typescript.
61+
open http://localhost:8888/simple_ext1/typescript
62+
# Error content.
63+
open http://localhost:8888/simple_ext1/nope
64+
# Redirect.
65+
open http://localhost:8888/simple_ext1/redirect
66+
# Favicon static content.
67+
open http://localhost:8888/static/simple_ext1/favicon.ico
68+
```
69+
70+
## Extension 1 and Extension 2
71+
72+
The following command starts both the `simple_ext1` and `simple_ext2` extensions.
73+
74+
```bash
75+
# Start the jupyter server, it will load both simple_ext1 and simple_ext2 based on the provided trait.
76+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}"
77+
```
78+
79+
Check that the previous `Extension 1` content is still available ant that you can also render `Extension 2` Server content in your browser.
80+
81+
```bash
82+
# HTML static page.
83+
open http://localhost:8888/static/simple_ext2/test.html
84+
# Content from Handlers.
85+
open http://localhost:8888/simple_ext2/params/test?var1=foo
86+
```
87+
88+
## Work with Entrypoints
89+
90+
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.
91+
92+
```bash
93+
pip uninstall -y jupyter_simple_ext && \
94+
python setup.py install && \
95+
cp -r ./etc $(dirname $(which jupyter))/..
96+
# Start the jupyter server extension simple_ext1, it will also load simple_ext2 because of load_other_extensions = True..
97+
# When you invoke with the entrypoint, the default url will be opened in your browser.
98+
jupyter simple-ext1
99+
```
100+
101+
## Configuration
102+
103+
Stop any running server (with `CTRL+C`) and start with additional configuration on the command line.
104+
105+
The provided settings via CLI will override the configuration that reside in the files (`jupyter_simple_ext1_config.py`...)
106+
107+
```bash
108+
jupyter simple-ext1 --SimpleApp1.configA="ConfigA from command line"
109+
```
110+
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`.
112+
113+
```
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'}}
119+
```
120+
121+
## Only Extension 2
122+
123+
Now stop agin the server and start with only `Extension 2`.
124+
125+
```bash
126+
# Start the jupyter server extension simple_ext2, it will NOT load simple_ext1 because of load_other_extensions = False.
127+
jupyter simple-ext2
128+
```
129+
130+
Try with the above links to check that only Extension 2 is responding (Extension 1 URLs should give you an 404 error).
131+
132+
## Extension 11 extends Extension 1
133+
134+
`Extension 11` extends `Extension 1` and brings a few more configs.
135+
136+
Run `jupyter simple-ext11 --generate-config && vi ~/.jupyter/jupyter_config.py`.
137+
138+
> TODO `--generate-config` returns an exception `"The ExtensionApp has not ServerApp "`
139+
140+
The generated configuration should contains the following.
141+
142+
```bash
143+
TBD
144+
```
145+
146+
The `hello`, `ignore_js` and `simple11_dir` are traits defined on the SimpleApp11 class.
147+
148+
It also implements additional flags and aliases for these traits.
149+
150+
+ The `--hello` flag will log on startup `Hello Simple11 - You have provided the --hello flag or defined a c.SimpleApp1.hello == True`.
151+
+ The `--simple11-dir` alias will set `SimpleExt11.simple11_dir` settings.
152+
153+
Stop any running server and then start the simple-ext11.
154+
155+
```bash
156+
jupyter simple-ext11 --hello --simple11-dir any_folder
157+
# TODO FIX the following command, simple11 does not work launching with jpserver_extensions parameter.
158+
jupyter server --ServerApp.jpserver_extensions="{'simple_ext11': True}" --hello --simple11-dir any_folder
159+
```
160+
161+
Ensure the following URLs respond correctly.
162+
163+
```bash
164+
# Jupyter Server Home Page.
165+
open http://localhost:8888/
166+
# TODO Fix Default URL, it does not show on startup.
167+
# Home page as defined by default_url = '/default'.
168+
open http://localhost:8888/simple_ext11/default
169+
# HTML static page.
170+
open http://localhost:8888/static/simple_ext11/test.html
171+
# Content from Handlers.
172+
open http://localhost:8888/simple_ext11/params/test?var1=foo
173+
# Content from Template.
174+
open http://localhost:8888/simple_ext11/template1/test
175+
# Content from Template with Typescript.
176+
open http://localhost:8888/simple_ext11/typescript
177+
# Error content.
178+
open http://localhost:8888/simple_ext11/nope
179+
# Redirect.
180+
open http://localhost:8888/simple_ext11/redirect
181+
# Favicon static content.
182+
open http://localhost:8888/static/simple_ext11/favicon.ico
183+
```
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_ext1": true
5+
}
6+
}
7+
}
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"ServerApp": {
3+
"jpserver_extensions": {
4+
"simple_ext2": true
5+
}
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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_Example'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c.SimpleApp11.ignore_js = True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c.SimpleApp2.configD = 'ConfigD from file'

examples/simple/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "jupyter-simple-ext",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"build": "tsc -p src && webpack",
7+
"clean": "rimraf build",
8+
"prepublishOnly": "npm run build"
9+
},
10+
"dependencies": {},
11+
"devDependencies": {
12+
"rifraf": "2.0.3",
13+
"webpack": "~4.29.6",
14+
"webpack-cli": "^3.3.0",
15+
"whatwg-fetch": "~2.0.3",
16+
"typescript": "3.6.4"
17+
}
18+
}

0 commit comments

Comments
 (0)