• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pyta-uoft / pyta / 12943845089

24 Jan 2025 05:27AM UTC coverage: 92.314% (-0.2%) from 92.514%
12943845089

Pull #1135

github

web-flow
Merge cab30de5e into a02e735ac
Pull Request #1135: Added Watch Property to HTML Server

16 of 31 new or added lines in 2 files covered. (51.61%)

7 existing lines in 1 file now uncovered.

3219 of 3487 relevant lines covered (92.31%)

13.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.28
/python_ta/reporters/html_server.py
1
import os
15✔
2
import sys
15✔
3
import webbrowser
15✔
4
from http.server import BaseHTTPRequestHandler, HTTPServer
15✔
5

6

7
def open_html_in_browser(html: bytes, watch: bool) -> None:
15✔
8
    """
9
    Display html in a web browser without creating a temp file.
10
    Instantiates a trivial http server and uses the webbrowser module to
11
    open a URL to retrieve html from that server.
12

13
    If watch is False, the server responds to exactly one request and then shuts down.
14
    If watch is True, the server runs indefinitely, allowing multiple requests.
15

16
    Adapted from: https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/io/_base_renderers.py#L655
17
    """
18

19
    class RequestHandler(BaseHTTPRequestHandler):
15✔
20
        def do_GET(self):
15✔
NEW
21
            self.send_response(200)
×
NEW
22
            self.send_header("Content-type", "text/html")
×
NEW
23
            self.end_headers()
×
24

NEW
25
            buffer_size = 1024 * 1024
×
NEW
26
            for i in range(0, len(html), buffer_size):
×
NEW
27
                self.wfile.write(html[i : i + buffer_size])
×
28

29
        def log_message(self, format, *args):
15✔
30
            """Overridden so that no server logging is printed."""
NEW
31
            pass
×
32

33
    if watch:
15✔
NEW
34
        print(
×
35
            "[INFO] Your PythonTA report is being opened in your web browser.\n"
36
            "       Press Ctrl + C, or the stop console button on your IDE, to exit the server.\n\n"
37
            "       If it doesn't open, please add an output argument to python_ta.check_all\n"
38
            "       as follows:\n\n"
39
            "         check_all(..., output='pyta_report.html')\n\n"
40
            "       This will cause PythonTA to save the report to a file, pyta_report.html,\n"
41
            "       that you can open manually in a web browser.",
42
            file=sys.stderr,
43
        )
NEW
44
        server = HTTPServer(("127.0.0.1", 0), RequestHandler)
×
NEW
45
        webbrowser.open(f"http://127.0.0.1:{server.server_port}", new=2)
×
46

NEW
47
        try:
×
NEW
48
            server.serve_forever()
×
NEW
49
        except KeyboardInterrupt:
×
NEW
50
            print("\nShutting down the PythonTA server.")
×
NEW
51
            server.server_close()
×
52
    else:
53
        server = HTTPServer(("127.0.0.1", 0), RequestHandler)
15✔
54
        webbrowser.open(f"http://127.0.0.1:{server.server_port}", new=2)
15✔
55
        server.handle_request()
15✔
56
        server.server_close()
15✔
57
        print(
15✔
58
            "[INFO] Your PythonTA report is being opened in your web browser.\n"
59
            "       If it doesn't open, please add an output argument to python_ta.check_all\n"
60
            "       as follows:\n\n"
61
            "         check_all(..., output='pyta_report.html')\n\n"
62
            "       This will cause PythonTA to save the report to a file, pyta_report.html,\n"
63
            "       that you can open manually in a web browser.",
64
            file=sys.stderr,
65
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc