Skip to content

Commit 22025a8

Browse files
committed
Start doc server if local doc is available
1 parent 58d239a commit 22025a8

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/bin/sage-notebook

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,30 +186,24 @@ def trac_23428_browser_workaround():
186186

187187
@contextmanager
188188
def sage_doc_server():
189-
from sage.env import SAGE_DOC_SERVER_URL
190-
191-
if SAGE_DOC_SERVER_URL:
192-
print(f'Sage doc server running at {SAGE_DOC_SERVER_URL}')
193-
yield
194-
else:
195-
from functools import partial
196-
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
197-
from threading import Thread
189+
from functools import partial
190+
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
191+
from threading import Thread
198192

199-
from sage.env import SAGE_DOC, SAGE_DOC_LOCAL_PORT as port
193+
from sage.env import SAGE_DOC, SAGE_DOC_LOCAL_PORT as port
200194

201-
server = ThreadingHTTPServer(('localhost', int(port)),
202-
partial(SimpleHTTPRequestHandler, directory=SAGE_DOC))
203-
server_thread = Thread(target=server.serve_forever, name="sage_doc_server")
204-
server_thread.start()
205-
print(f'Sage doc server started running at http://localhost:{port}')
195+
server = ThreadingHTTPServer(('localhost', int(port)),
196+
partial(SimpleHTTPRequestHandler, directory=SAGE_DOC))
197+
server_thread = Thread(target=server.serve_forever, name="sage_doc_server")
198+
server_thread.start()
199+
print(f'Sage doc server started running at http://localhost:{port}')
206200

207-
try:
208-
yield
209-
finally:
210-
server.shutdown()
211-
server_thread.join()
212-
print(f'Sage doc server stopped runnning at http://localhost:{port}')
201+
try:
202+
yield
203+
finally:
204+
server.shutdown()
205+
server_thread.join()
206+
print(f'Sage doc server stopped runnning at http://localhost:{port}')
213207

214208

215209
if __name__ == '__main__':
@@ -254,5 +248,17 @@ if __name__ == '__main__':
254248

255249
banner()
256250

257-
with sage_doc_server():
251+
# Start a Sage doc server if the Sage documentation is available locally.
252+
# See the corresponding code in src/sage/repl/ipython_kernel/kernel.py.
253+
254+
from sage.env import SAGE_DOC_SERVER_URL
255+
from sage.features.sagemath import sagemath_doc_html
256+
257+
if SAGE_DOC_SERVER_URL:
258+
print(f'Sage doc server running at {SAGE_DOC_SERVER_URL}')
259+
launcher(unknown)
260+
elif sagemath_doc_html().is_present():
261+
with sage_doc_server():
262+
launcher(unknown)
263+
else:
258264
launcher(unknown)

src/sage/repl/ipython_kernel/kernel.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,24 @@ def help_links(self):
103103
'url': '.../html/en/index.html'},
104104
...]
105105
"""
106-
from sage.env import SAGE_DOC_SERVER_URL as url
106+
# A Sage doc server starts when Jupyter notebook launches if the Sage
107+
# documentation is available locally. See the corresponding code in
108+
# src/bin/sage-notebook.
107109

108-
if url:
110+
from sage.env import SAGE_DOC_SERVER_URL
111+
from sage.features.sagemath import sagemath_doc_html
112+
113+
if SAGE_DOC_SERVER_URL:
109114
def doc_url(path):
110-
return '{}/{}'.format(url, path)
111-
else:
115+
return f'{SAGE_DOC_SERVER_URL}/{path}'
116+
elif sagemath_doc_html().is_present():
112117
from sage.env import SAGE_DOC_LOCAL_PORT as port
113118

114119
def doc_url(path):
115-
return 'http://localhost:{}/{}'.format(port, path)
120+
return f'http://localhost:{port}/{path}'
121+
else:
122+
def doc_url(path):
123+
return f'https://doc.sagemath.org/{path}'
116124

117125
return [
118126
{

0 commit comments

Comments
 (0)