Pylint JSON to HTML Converter

0.5.0 · active · verified Thu Apr 16

pylint-json2html is a Python utility that transforms Pylint's JSON report output into a human-readable HTML document. This library addresses the removal of native HTML output from Pylint since version 1.7. It provides a simple command-line interface to generate presentable code quality reports. The current version is 0.5.0, with releases occurring as needed rather than on a fixed cadence.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates the typical workflow: first, run Pylint on your code, ensuring its output format is set to JSON. Then, pipe this JSON output to `pylint-json2html`, redirecting its HTML output to a specified file. Alternatively, Pylint's output can be saved to a file and passed as an argument to `pylint-json2html`.

import subprocess
import os

# Create a dummy Python file to lint
with open('my_module.py', 'w') as f:
    f.write('def my_func():\n    """Missing docstring for function."""\n    pass\n')

# 1. Run Pylint to generate a JSON report
# We use a temporary file for Pylint's output, then read it.
# In a real scenario, you'd pipe directly: `pylint my_module.py --output-format=json | pylint-json2html -o report.html`
try:
    pylint_cmd = ['pylint', 'my_module.py', '--output-format=json', '--msg-template="{path}:{line}:{column}: {msg_id}: {msg} ({symbol})"']
    pylint_process = subprocess.run(pylint_cmd, capture_output=True, text=True, check=True)
    pylint_json_output = pylint_process.stdout
    
    # Save Pylint's JSON output to a temporary file for demonstration
    with open('pylint_report.json', 'w') as f:
        f.write(pylint_json_output)

    # 2. Convert the JSON report to HTML using pylint-json2html
    # Note: pylint-json2html is typically used via piping, but for programmatic demo, 
    # we simulate input from a file or directly pass content if possible (which it is via stdin for the CLI tool).
    # For this example, we'll run it as a separate command reading the file.
    html_cmd = ['pylint-json2html', 'pylint_report.json', '-o', 'pylint_report.html']
    subprocess.run(html_cmd, check=True)

    print("HTML report generated at pylint_report.html")
    print("JSON report saved at pylint_report.json")

except subprocess.CalledProcessError as e:
    print(f"An error occurred during pylint or pylint-json2html execution: {e.stderr}")
except FileNotFoundError:
    print("Error: Pylint or pylint-json2html command not found. Ensure they are installed and in your PATH.")
finally:
    # Clean up dummy files
    if os.path.exists('my_module.py'):
        os.remove('my_module.py')
    if os.path.exists('pylint_report.json'):
        os.remove('pylint_report.json')
    # The generated HTML report is left for inspection
    # if os.path.exists('pylint_report.html'):
    #     os.remove('pylint_report.html') # Uncomment to remove HTML as well

view raw JSON →