serve.py
55 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
damianavila
|
r11773 | """ | ||
Contains postprocessor for serving nbconvert output. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
#Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
#Distributed under the terms of the Modified BSD License. | ||||
# | ||||
#The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
import os | ||||
damianavila
|
r11804 | import webbrowser | ||
damianavila
|
r11773 | from BaseHTTPServer import HTTPServer | ||
from SimpleHTTPServer import SimpleHTTPRequestHandler | ||||
Martijn Vermaat
|
r11880 | from IPython.utils.traitlets import Bool | ||
damianavila
|
r11773 | |||
from .base import PostProcessorBase | ||||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
class ServePostProcessor(PostProcessorBase): | ||||
"""Post processor designed to serve files""" | ||||
damianavila
|
r11807 | open_in_browser = Bool(True, config=True, | ||
Martijn Vermaat
|
r11880 | help="""Set to False to deactivate | ||
damianavila
|
r11807 | the opening of the browser""") | ||
damianavila
|
r11773 | |||
def call(self, input): | ||||
""" | ||||
Simple implementation to serve the build directory. | ||||
""" | ||||
Martijn Vermaat
|
r11880 | |||
damianavila
|
r11775 | try: | ||
Martijn Vermaat
|
r11880 | dirname, filename = os.path.split(input) | ||
Jonathan Frederic
|
r12052 | if dirname: | ||
os.chdir(dirname) | ||||
damianavila
|
r11775 | httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler) | ||
sa = httpd.socket.getsockname() | ||||
Martijn Vermaat
|
r11880 | url = "http://" + sa[0] + ":" + str(sa[1]) + "/" + filename | ||
damianavila
|
r11807 | if self.open_in_browser: | ||
webbrowser.open(url, new=2) | ||||
Martijn Vermaat
|
r11880 | print("Serving your slides on " + url) | ||
damianavila
|
r11775 | print("Use Control-C to stop this server.") | ||
httpd.serve_forever() | ||||
except KeyboardInterrupt: | ||||
print("The server is shut down.") | ||||