QuantEcon / jupinx

Jupyter + Sphinx Utilities and Tools
https://quantecon.github.io/jupinx/
20 stars 4 forks source link

FEAT: add preview option for notebooks and website #29

Closed mmcky closed 5 years ago

mmcky commented 5 years ago

This PR adds:

  1. -v, --view option to start a server to view results. Default value = "notebooks". This is an additional option that should be specified after the directory.

Usage Examples:

jupinx -n lecture_source_py --view
jupinx -n lecture_source_py --view=notebooks
jupinx -w lecture_source_py --view=website

I have not added support for lecture specific targeting but could be added as a second argument.

mmcky commented 5 years ago

@AakashGfude this works -- but the subprocess call doesn't exit cleanly when closing the notebook server.

Serving notebooks from local directory: /home/mmcky/tmp/lecture-source-py/_build/jupyter
0 active kernels
The Jupyter Notebook is running at:
http://localhost:8892/?token=d8a89de146c8c7842747265e954009f30bdaccb58ec8fbfa
Shutdown this notebook server (y/[n])? Traceback (most recent call last):
  File "/home/mmcky/anaconda3/bin/jupinx", line 11, in <module>
    load_entry_point('Jupinx', 'console_scripts', 'jupinx')()
  File "/home/mmcky/repos-collab/jupinx/jupinx/cmd/build.py", line 176, in main
    make_file_actions(d)
  File "/home/mmcky/repos-collab/jupinx/jupinx/cmd/build.py", line 136, in make_file_actions
    handle_make_preview(arg_dict)
  File "/home/mmcky/repos-collab/jupinx/jupinx/cmd/build.py", line 112, in handle_make_preview
    subprocess.run(cmd, cwd=arg_dict['directory'])
  File "/home/mmcky/anaconda3/lib/python3.7/subprocess.py", line 474, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/home/mmcky/anaconda3/lib/python3.7/subprocess.py", line 931, in communicate
    self.wait()
  File "/home/mmcky/anaconda3/lib/python3.7/subprocess.py", line 990, in wait
    return self._wait(timeout=timeout)
  File "/home/mmcky/anaconda3/lib/python3.7/subprocess.py", line 1624, in _wait
    (pid, sts) = self._try_wait(0)
  File "/home/mmcky/anaconda3/lib/python3.7/subprocess.py", line 1582, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
(base) mmcky@mmcky-desktop ~/tmp $ No answer for 5s: resuming operation...

any ideas on how we can get it to respect the Ctrl-c?

mmcky commented 5 years ago

fixes #29

mmcky commented 5 years ago
mmcky commented 5 years ago

@AakashGfude I think I have figured this out. I used Popen to open a child process asynchronously and then implemented a KeyboardInterrupt exception to catch Ctrl-C and gracefully kill the notebook server and website servers.

mmcky commented 5 years ago
(base) mmcky@mmcky-desktop ~/tmp/lecture-source-py $ jupinx --view=notebooks
notebooks
Running: make preview

To close the server press Ctrl-C

cd _build/jupyter/ && jupyter notebook
WebIO config in ~/.jupyter/jupyter_notebook_config.py but WebIO plugin not found
[I 14:22:23.701 NotebookApp] JupyterLab extension loaded from /home/mmcky/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 14:22:23.701 NotebookApp] JupyterLab application directory is /home/mmcky/anaconda3/share/jupyter/lab
[I 14:22:23.703 NotebookApp] Serving notebooks from local directory: /home/mmcky/tmp/lecture-source-py/_build/jupyter
[I 14:22:23.703 NotebookApp] The Jupyter Notebook is running at:
[I 14:22:23.703 NotebookApp] http://localhost:8888/?token=400fa7c932ecd33c5004f5ae3ef10cbc8bd8153f260c335a
[I 14:22:23.703 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 14:22:23.715 NotebookApp] 

    To access the notebook, open this file in a browser:
        file:///run/user/1000/jupyter/nbserver-15834-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=400fa7c932ecd33c5004f5ae3ef10cbc8bd8153f260c335a
^C[I 14:22:27.935 NotebookApp] interrupted
Serving notebooks from local directory: /home/mmcky/tmp/lecture-source-py/_build/jupyter
0 active kernels
The Jupyter Notebook is running at:
http://localhost:8888/?token=400fa7c932ecd33c5004f5ae3ef10cbc8bd8153f260c335a
Shutdown this notebook server (y/[n])? Shutting down server on port 8888 ...
[I 14:22:28.586 NotebookApp] Shutting down on /api/shutdown request.
[I 14:22:28.587 NotebookApp] Shutting down 0 kernels

Closing notebooks server ...

the weakness of this approach is the notebook server on port 8888 get's stopped. So if a user is running multiple notebook servers then another jupyter notebook app may get stopped. Can you see an easy way to capture the port that is used in the Popen command and pass that port number when handling the stop process.

mmcky commented 5 years ago

perhaps we should capture the output and look for the pattern http://localhost:8888/?token=400fa7c932ecd33c5004f5ae3ef10cbc8bd8153f260c335a and infer the port after localhost.

mmcky commented 5 years ago

@jstac from a user perspective do you ever run multiple jupyter notebook servers at once? Just trying to understand if this is a big issue or not. I think it is certainly one that needs fixing but wondering if we do it now or later.

jstac commented 5 years ago

Actually I do run multiple servers at once, sometimes...

mmcky commented 5 years ago

thanks @jstac I figured we should make this a bit more robust. I have adjusted the code to use a default port of 8890 so you would need 12 open before a collision occurs. The PORT can also be specified (but not via an option through the CLI) if we need to adjust later on.