Spaces:
Running
Running
File size: 1,924 Bytes
fb7582f |
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 |
import gradio as gr
from flake8.api import legacy as flake8
import autopep8
def lint_code(code: str) -> tuple[str, str]:
"""
Lint the provided Python code using Flake8 and auto-fix it with autopep8.
Returns the fixed code and the linting report.
"""
# Run Flake8 for linting, ignoring line-length violations (E501)
style_guide = flake8.get_style_guide(ignore=["E501"])
report = style_guide.check_files(None, lines=code.splitlines())
# Auto-fix the code with autopep8
fixed_code = autopep8.fix_code(code)
# Generate the linting report
lint_report = "\n".join(msg for msg in report.get_statistics())
return fixed_code, lint_report
def process_file(file) -> tuple[str, str]:
"""
Process the uploaded file, decode its content, and lint it.
Returns the fixed code and linting report.
"""
# Read the file content and decode it to a string
code = file.read().decode("utf-8")
# Lint the code and get the report
return lint_code(code)
def handle_input(code: str, file) -> tuple[str, str]:
"""
Handle both code inputs: either text or file.
If a file is provided, process the file. Otherwise, process the pasted code.
"""
if file:
return process_file(file)
return lint_code(code)
# Create the Gradio interface
interface = gr.Interface(
fn=handle_input,
inputs=[
gr.Textbox(lines=20, placeholder="Paste your Python code here...", label="Python Code"),
gr.File(label="Upload a Python (.py) file", file_types=[".py"])
],
outputs=[
gr.Textbox(label="Linted Code", lines=20),
gr.Textbox(label="Linting Report")
],
title="Python Code Linter",
description="Paste your Python code or upload a .py file to get a Flake8-compliant, linted version."
)
# Launch the interface
interface.launch() |