Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/34794/run doc server along with jupyter #8

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/bin/sage-notebook
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import ast
import argparse
import logging
import textwrap

from contextlib import contextmanager

logging.basicConfig()
logger = logging.getLogger()

from sage.misc.banner import banner


_system_jupyter_url = "https://doc.sagemath.org/html/en/installation/launching.html#setting-up-sagemath-as-a-jupyter-kernel-in-an-existing-jupyter-notebook-or-jupyterlab-installation"


class NotebookJupyter():

def print_banner(self):
banner()
print('Please wait while the Sage Jupyter Notebook server starts...')

@classmethod
Expand All @@ -45,7 +46,6 @@ class NotebookJupyter():

class NotebookJupyterlab():
def print_banner(self):
banner()
print('Please wait while the Jupyterlab server starts...')

@classmethod
Expand All @@ -71,7 +71,6 @@ class NotebookJupyterlab():

class NotebookNbclassic():
def print_banner(self):
banner()
print('Please wait while the Jupyterlab server starts...')

@classmethod
Expand All @@ -94,7 +93,6 @@ class NotebookNbclassic():

class NotebookRetrolab():
def print_banner(self):
banner()
print('Please wait while the Jupyterlab server starts...')

@classmethod
Expand All @@ -118,7 +116,6 @@ class NotebookRetrolab():
class SageNBExport(NotebookJupyter):

def print_banner(self):
banner()
print('Please wait while the SageNB export server starts...')

@classmethod
Expand Down Expand Up @@ -172,7 +169,6 @@ EXAMPLES:

"""


notebook_launcher = {
'default': NotebookJupyter, # change this to change the default
'ipython': NotebookJupyter,
Expand All @@ -183,7 +179,6 @@ notebook_launcher = {
'export': SageNBExport,
}


notebook_names = ', '.join(notebook_launcher.keys())


Expand Down Expand Up @@ -230,6 +225,34 @@ def trac_23428_browser_workaround():
os.environ['BROWSER'] = 'open'


@contextmanager
def sage_doc_server():
from sage.env import SAGE_DOC_SERVER_URL

if SAGE_DOC_SERVER_URL:
print(f'Sage doc server running at {SAGE_DOC_SERVER_URL}')
yield
else:
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread

from sage.env import SAGE_DOC, SAGE_DOC_LOCAL_PORT as port

server = ThreadingHTTPServer(('localhost', int(port)),
partial(SimpleHTTPRequestHandler, directory=SAGE_DOC))
server_thread = Thread(target=server.serve_forever, name="sage_doc_server")
server_thread.start()
print(f'Sage doc server started running at http://localhost:{port}')

try:
yield
finally:
server.shutdown()
server_thread.join()
print(f'Sage doc server stopped runnning at http://localhost:{port}')


if __name__ == '__main__':
parser = make_parser()
args, unknown = parser.parse_known_args(sys.argv[1:])
Expand Down Expand Up @@ -270,4 +293,7 @@ if __name__ == '__main__':
launcher.print_help()
sys.exit(0)

launcher(unknown)
banner()

with sage_doc_server():
launcher(unknown)
4 changes: 4 additions & 0 deletions src/sage/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def var(key: str, *fallbacks: Optional[str], force: bool = False) -> Optional[st
SAGE_PKGS = var("SAGE_PKGS", join(SAGE_ROOT, "build", "pkgs"))
SAGE_ROOT_GIT = var("SAGE_ROOT_GIT", join(SAGE_ROOT, ".git"))

# Sage doc server (local server with PORT if URL is not given)
SAGE_DOC_SERVER_URL = var("SAGE_DOC_SERVER_URL")
SAGE_DOC_LOCAL_PORT = var("SAGE_DOC_LOCAL_PORT", "8000")

# ~/.sage
DOT_SAGE = var("DOT_SAGE", join(os.environ.get("HOME"), ".sage"))
SAGE_STARTUP_FILE = var("SAGE_STARTUP_FILE", join(DOT_SAGE, "init.sage"))
Expand Down
30 changes: 19 additions & 11 deletions src/sage/repl/ipython_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,40 +93,48 @@ def help_links(self):
sage: sk = SageKernel.__new__(SageKernel)
sage: sk.help_links
[{'text': 'Sage Documentation',
'url': 'kernelspecs/sagemath/doc/html/en/index.html'},
'url': 'http://localhost:8000/html/en/index.html'},
...]
"""
from sage.repl.ipython_kernel.install import SageKernelSpec
identifier = SageKernelSpec.identifier()
kernel_url = lambda x: 'kernelspecs/{0}/{1}'.format(identifier, x)
from sage.env import SAGE_DOC_SERVER_URL as url

if url:
def doc_url(path):
return '{}/{}'.format(url, path)
else:
from sage.env import SAGE_DOC_LOCAL_PORT as port

def doc_url(path):
return 'http://localhost:{}/{}'.format(port, path)

return [
{
'text': 'Sage Documentation',
'url': kernel_url('doc/html/en/index.html'),
'url': doc_url("html/en/index.html"),
},
{
'text': 'Tutorial',
'url': kernel_url('doc/html/en/tutorial/index.html'),
'url': doc_url('html/en/tutorial/index.html'),
},
{
'text': 'Thematic Tutorials',
'url': kernel_url('doc/html/en/thematic_tutorials/index.html'),
'url': doc_url('html/en/thematic_tutorials/index.html'),
},
{
'text': 'FAQs',
'url': kernel_url('doc/html/en/faq/index.html'),
'url': doc_url('html/en/faq/index.html'),
},
{
'text': 'PREP Tutorials',
'url': kernel_url('doc/html/en/prep/index.html'),
'url': doc_url('html/en/prep/index.html'),
},
{
'text': 'Reference',
'url': kernel_url('doc/html/en/reference/index.html'),
'url': doc_url('html/en/reference/index.html'),
},
{
'text': "Developer's Guide",
'url': kernel_url('doc/html/en/developer/index.html'),
'url': doc_url('html/en/developer/index.html'),
},
{
'text': "Python",
Expand Down