Spaces:
Running
Running
import gradio as gr | |
import flake8.main.application | |
import autopep8 | |
import tempfile | |
import os | |
def lint_code(code): | |
# Create a temporary file to store the code | |
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file: | |
temp_file.write(code) | |
temp_file_path = temp_file.name | |
try: | |
# Run Flake8 | |
app = flake8.main.application.Application() | |
app.initialize(['--format=default', temp_file_path]) | |
app.run_checks() | |
app.generate_reports() | |
flake8_output = app.results_manager.stdout.getvalue().strip() | |
# Format the code using AutoPEP8 | |
formatted_code = autopep8.fix_code(code) | |
return flake8_output, formatted_code | |
except Exception as e: | |
return f"An error occurred: {str(e)}", code # Return original code on error | |
finally: | |
# Clean up the temporary file | |
os.remove(temp_file_path) | |
def main(): | |
with gr.Interface( | |
fn=lint_code, | |
inputs=gr.Textbox(lines=10, placeholder="Enter your Python code here..."), | |
outputs=[gr.Textbox(label="Flake8 Output"), gr.Code(label="Formatted Code", language="python")], | |
title="PyLintPro", | |
description="A Gradio app that helps users improve Python code to meet Flake8 and PEP 8 standards.", | |
) as demo: | |
demo.launch() | |
if __name__ == "__main__": | |
main() |