docs for pattern_lens v0.2.0
View Source on GitHub

pattern_lens.server

cli for starting the server to show the web ui.

can also run with --rewrite-index to update the index.html file. this is useful for working on the ui.


 1"""cli for starting the server to show the web ui.
 2
 3can also run with --rewrite-index to update the index.html file. this is useful for working on the ui.
 4"""
 5
 6from pathlib import Path
 7import sys
 8import os
 9import argparse
10import http.server
11import socketserver
12
13from pattern_lens.indexes import write_html_index
14
15
16def main(path: str, port: int = 8000):
17    os.chdir(path)
18    try:
19        with socketserver.TCPServer(
20            ("", port), http.server.SimpleHTTPRequestHandler
21        ) as httpd:
22            print(f"Serving at http://localhost:{port}")
23            httpd.serve_forever()
24    except KeyboardInterrupt:
25        print("Server stopped")
26        sys.exit(0)
27
28
29if __name__ == "__main__":
30    arg_parser: argparse.ArgumentParser = argparse.ArgumentParser()
31    arg_parser.add_argument(
32        "--path",
33        type=str,
34        required=False,
35        help="The path to serve, defaults to the current directory",
36        default=".",
37    )
38    arg_parser.add_argument(
39        "--port",
40        type=int,
41        required=False,
42        help="The port to serve on, defaults to 8000",
43        default=8000,
44    )
45    arg_parser.add_argument(
46        "--rewrite-index",
47        action="store_true",
48        help="Whether to write the latest index.html file",
49    )
50    args: argparse.Namespace = arg_parser.parse_args()
51
52    if args.rewrite_index:
53        write_html_index(path=Path(args.path))
54
55    main(path=args.path, port=args.port)

def main(path: str, port: int = 8000):
17def main(path: str, port: int = 8000):
18    os.chdir(path)
19    try:
20        with socketserver.TCPServer(
21            ("", port), http.server.SimpleHTTPRequestHandler
22        ) as httpd:
23            print(f"Serving at http://localhost:{port}")
24            httpd.serve_forever()
25    except KeyboardInterrupt:
26        print("Server stopped")
27        sys.exit(0)