This repository has been archived by the owner on Jul 28, 2019. It is now read-only.
/
imarkdownapp.py
95 lines (78 loc) · 3.42 KB
/
imarkdownapp.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from subprocess import Popen
from notebook.notebookapp import NotebookApp, flags
from traitlets import Unicode, Bool
import time
# from . import EXT_NAME, PACKAGE_DIR
from config import IMarkdownConfig
from extension import load_jupyter_server_extension
# from .utils import cmd_in_new_dir
import os
from contextlib import contextmanager
@contextmanager
def cmd_in_new_dir(newdir):
cur_dir = os.getcwd()
os.chdir(newdir)
yield
os.chdir(cur_dir)
PACKAGE_DIR = os.path.realpath(os.path.dirname(__file__))
EXT_NAME = 'imarkdown'
webpack_port = 8357
webpack_hot = {"address": 'http://localhost:{webpack_port}/'.format(webpack_port=webpack_port),
"command": ["yarn", "start", "--port", str(webpack_port)]}
nteract_flags = dict(flags)
nteract_flags['dev'] = (
{'NteractConfig': {'asset_url': webpack_hot['address']},
'NteractApp': {'dev_mode': True}
},
"\n".join([
"Start nteract in dev mode, serving assets built from your source code.",
"This is a hot reloading server that watches for changes to your source,",
"rebuilds the js files, and serves the new assets on:",
" {address}",
"To access this server run:",
" `{command}`"]).format(address=webpack_hot["address"],
command=" ".join(webpack_hot["command"])
)
)
class IMarkdownApp(NotebookApp):
"""Application for runing nteract on a jupyter notebook server.
"""
default_url = Unicode('/imarkdown/edit',
help="nteract's default starting location")
classes = [*NotebookApp.classes, IMarkdownConfig]
flags = nteract_flags
dev_mode = Bool(True, config=True,
help="""Whether to start the app in dev mode. Expects resources to be loaded
from webpack's hot reloading server at {address}. Run
`{command}`
To serve your assets.
This is only useful if NteractApp is installed editably e.g., using `pip install -e .`.
""".format(address=webpack_hot["address"],
command=" ".join(webpack_hot["command"])))
def init_server_extensions(self):
super(IMarkdownApp, self).init_server_extensions()
if self.dev_mode:
msg = 'IMarkdown server extension not enabled, loading in dev mode...'
if not self.nbserver_extensions.get(EXT_NAME, False):
self.log.warn(msg)
load_jupyter_server_extension(self)
with cmd_in_new_dir(PACKAGE_DIR):
p = Popen(webpack_hot["command"])
self.log.info('waiting for the hot webpack server to start')
# Wait a little bit to allow the initial command to run
# NOTE: It would be better if we could run this on a thread and
# inform the dev server when it has closed using proc.wait()
# and an onExit callback
time.sleep(3)
exit_code = p.poll()
if exit_code is None:
# The process is up, we're (possibly) good
pass
else:
raise Exception(
"Webpack dev server exited - return code {exit_code}".format(exit_code=exit_code))
# Now wait for webpack to have the initial bundle mostly ready
time.sleep(5)
main = launch_new_instance = IMarkdownApp.launch_instance
if __name__ == '__main__':
main()