-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
93 lines (75 loc) · 2.66 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from http.server import HTTPServer, SimpleHTTPRequestHandler
import sys
import os
# GLOBALS
build = True
silent = False
directory = "book/_build/html"
geosmart = r"""
_____ _____ __ __ _____ _______
/ ____| / ____| \/ | /\ | __ \__ __|
| | __ ___ ___| (___ | \ / | / \ | |__) | | |
| | |_ |/ _ \/ _ \\___ \| |\/| | / /\ \ | _ / | |
| |__| | __/ (_) |___) | | | |/ ____ \| | \ \ | |
\_____|\___|\___/_____/|_| |_/_/ \_\_| \_\ |_|
"""
def output(data):
if not silent:
print(data)
# Found this funky little function on stack overflow:
# https://stackoverflow.com/questions/40419276/python-how-to-print-text-to-console-as-hyperlink
def link(uri, label=None):
if label is None:
label = uri
parameters = ''
# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
escape_mask = '\033]8;{};{}\033\\{}\033]8;;\033\\'
return escape_mask.format(parameters, uri, label)
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
server_address = ("", 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
class NoExtensionHandler(SimpleHTTPRequestHandler):
def do_GET(self):
print("\nServing path:", self.path)
self.path = directory + self.path
print("Prepending directory:", self.path)
SimpleHTTPRequestHandler.do_GET(self)
def main():
global directory
if not silent:
clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
clear()
output(geosmart)
if build:
output("Attempting to build jupyter notebook:")
os.system("jb build book")
else:
output("Detected '--no-build' flag, skipping jb build.")
# path = os.getcwd()
# directory = ""
# sub_folders = ["binder", "book", "conda"]
# if any(folder in path for folder in sub_folders):
# output(f"Detected execution starting in non-root directory, adjusting relative paths.")
# directory = ".."
# else:
# output("Execution starting in root directory, using normal paths.")
# directory += "/book/_build/html"
output(f"Serving book from {directory} directory...")
output("Navigate to localhost:8000 in your browser to view the book!")
output("Or, if your terminal supports links: " + link("http://localhost:8000/"))
run(HTTPServer, NoExtensionHandler)
if len(sys.argv) == 1:
build = True
silent = False
main()
elif len(sys.argv) == 2:
flags = ["--no-build", "--nb"]
if sys.argv[1] in flags:
build = False
silent = False
main()
else:
print(f"Invalid flag provided. Expected one of {str(flags)}, got '{sys.argv[1]}'")
else:
print(f"Invalid number of arguments. Expected at most 1, got {len(sys.argv) - 1}")