flake8-html
flake8-html is a plugin for the flake8 linter that generates an HTML report of code violations, providing a visual and navigable overview of issues. The current version is 0.4.3. It has an infrequent release cadence, primarily updating to maintain compatibility with new versions of flake8.
Common errors
-
Error: unrecognized arguments: --format=html
cause The `flake8-html` plugin is not installed, or `flake8` cannot find it in the current environment.fixInstall `flake8-html` in the same Python environment as `flake8`: `pip install flake8-html`. -
No 'report.html' file was generated in the expected directory.
cause The `--htmldir` argument was either omitted or specified incorrectly, preventing `flake8-html` from saving the report to disk.fixEnsure you are using `--htmldir=<path/to/report>` to specify the output directory for the HTML report. -
The generated HTML report is empty or appears incomplete despite having linter violations.
cause This often occurs when `flake8-html < 0.4.3` is used with `flake8 >= 5.0.0` due to compatibility issues.fixUpgrade `flake8-html` to the latest version to ensure compatibility: `pip install --upgrade flake8-html`.
Warnings
- breaking Older versions of `flake8-html` (prior to 0.4.3) are incompatible with `flake8` versions 5.0.0 and above.
- gotcha For `flake8-html` to be discovered and used by `flake8`, it must be installed in the exact same Python environment (e.g., virtual environment) as `flake8` itself.
- gotcha Failing to specify the `--htmldir` argument will prevent the HTML report from being saved to a file, potentially causing the output to be printed to the console in an unreadable format or not generated at all.
Install
-
pip install flake8-html -
pip install flake8 flake8-html
Quickstart
import subprocess
import os
# Create a dummy Python file with a flake8 violation
code_to_lint = """
import os
def my_func( arg1, arg2 ):
print( os.path.join(arg1, arg2) )
"""
with open("test_code.py", "w") as f:
f.write(code_to_lint)
# Ensure the report directory exists
report_dir = "./flake8_html_report"
os.makedirs(report_dir, exist_ok=True)
# Run flake8 with the html format
print(f"Running: flake8 --format=html --htmldir={report_dir} .")
try:
result = subprocess.run(
["flake8", "--format=html", f"--htmldir={report_dir}", "."],
capture_output=True,
text=True,
check=True
)
print("Flake8 HTML report generated successfully.")
print(f"Report saved to: {os.path.abspath(report_dir)}/report.html")
except subprocess.CalledProcessError as e:
print(f"Error running flake8: {e}\n{e.stderr}")
except FileNotFoundError:
print("Error: 'flake8' command not found. Please ensure flake8 is installed.")
finally:
# Clean up dummy file
if os.path.exists("test_code.py"):
os.remove("test_code.py")
# Optionally, remove the report directory for a clean run next time
# import shutil
# if os.path.exists(report_dir):
# shutil.rmtree(report_dir)