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