This repository was archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path_scriptedforms_handlers.py
More file actions
65 lines (50 loc) · 1.79 KB
/
_scriptedforms_handlers.py
File metadata and controls
65 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import re
from glob import glob
from notebook.base.handlers import IPythonHandler, FileFindHandler
from notebook.utils import url_path_join as ujoin
from jinja2 import FileSystemLoader
from ._utilities import HERE
LOADER = FileSystemLoader(HERE)
def get_scriptedforms_handlers(base_url):
lib_dir = os.path.join(HERE, 'lib')
lib_files = glob(os.path.join(lib_dir, '*'))
rel_paths = [os.path.relpath(item, lib_dir) for item in lib_files]
lib_escaped = [re.escape(item) for item in rel_paths]
lib_strings = '|'.join(lib_escaped)
static_handler_regex = "/scriptedforms/static/({})".format(lib_strings)
scriptedforms_handlers = [
(
ujoin(base_url, r'/scriptedforms/use/.*\.md'),
ScriptedFormsHandler,
{'application_to_run': 'use'}
),
(
ujoin(base_url, static_handler_regex),
FileFindHandler,
{'path': lib_dir}
),
(
ujoin(base_url, r'/scriptedforms/docs'),
ScriptedFormsHandler,
{'application_to_run': 'docs'}
),
(
ujoin(base_url, r'/scriptedforms/docs/.*'),
ScriptedFormsHandler,
{'application_to_run': 'docs'}
)
]
return scriptedforms_handlers
class ScriptedFormsHandler(IPythonHandler):
def initialize(self, application_to_run):
self.application_to_run = application_to_run
def get(self):
return self.write(self.render_template(
"index.html",
base_url=self.base_url,
application_to_run=self.application_to_run))
def get_template(self, name):
return LOADER.load(self.settings['jinja2_env'], name)