Show More
@@ -1,54 +1,55 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Contains postprocessor for serving nbconvert output. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | #Copyright (c) 2013, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | #Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | #The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | import os |
|
17 | 17 | import webbrowser |
|
18 | 18 | |
|
19 | 19 | from BaseHTTPServer import HTTPServer |
|
20 | 20 | from SimpleHTTPServer import SimpleHTTPRequestHandler |
|
21 | 21 | |
|
22 | 22 | from IPython.utils.traitlets import Bool |
|
23 | 23 | |
|
24 | 24 | from .base import PostProcessorBase |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Classes |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | class ServePostProcessor(PostProcessorBase): |
|
30 | 30 | """Post processor designed to serve files""" |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | open_in_browser = Bool(True, config=True, |
|
34 | 34 | help="""Set to False to deactivate |
|
35 | 35 | the opening of the browser""") |
|
36 | 36 | |
|
37 | 37 | def call(self, input): |
|
38 | 38 | """ |
|
39 | 39 | Simple implementation to serve the build directory. |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | try: |
|
43 | 43 | dirname, filename = os.path.split(input) |
|
44 | if dirname: | |
|
44 | 45 | os.chdir(dirname) |
|
45 | 46 | httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler) |
|
46 | 47 | sa = httpd.socket.getsockname() |
|
47 | 48 | url = "http://" + sa[0] + ":" + str(sa[1]) + "/" + filename |
|
48 | 49 | if self.open_in_browser: |
|
49 | 50 | webbrowser.open(url, new=2) |
|
50 | 51 | print("Serving your slides on " + url) |
|
51 | 52 | print("Use Control-C to stop this server.") |
|
52 | 53 | httpd.serve_forever() |
|
53 | 54 | except KeyboardInterrupt: |
|
54 | 55 | print("The server is shut down.") |
General Comments 0
You need to be logged in to leave comments.
Login now