Canstralian commited on
Commit
a75b929
·
verified ·
1 Parent(s): 843046f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -2,23 +2,37 @@ import gradio as gr
2
  from flake8.api import legacy as flake8
3
  import autopep8
4
 
5
- def lint_code(code):
6
- # Run Flake8 for linting
7
- style_guide = flake8.get_style_guide(ignore=["E501"]) # Ignore line-length violations
 
8
  report = style_guide.check_files(None, lines=code.splitlines())
9
 
10
- # Auto-fix with autopep8
11
  fixed_code = autopep8.fix_code(code)
12
- return fixed_code, "\n".join(msg for msg in report.get_statistics())
 
 
 
13
 
14
- def process_file(file):
15
- # Read file contents
 
16
  code = file.read().decode("utf-8")
17
- linted_code, report = lint_code(code)
18
- return linted_code, report
 
 
 
 
 
 
 
 
19
 
 
20
  interface = gr.Interface(
21
- fn=lint_code,
22
  inputs=[
23
  gr.Textbox(lines=20, placeholder="Paste your Python code here...", label="Python Code"),
24
  gr.File(label="Upload a Python (.py) file", file_types=[".py"])
@@ -31,4 +45,5 @@ interface = gr.Interface(
31
  description="Paste your Python code or upload a .py file to get a Flake8-compliant, linted version."
32
  )
33
 
34
- interface.launch()
 
 
2
  from flake8.api import legacy as flake8
3
  import autopep8
4
 
5
+ def lint_code(code: str) -> tuple[str, str]:
6
+ """Lint the provided Python code and return the fixed code with linting report."""
7
+ # Run Flake8 for linting and ignore line-length violations
8
+ style_guide = flake8.get_style_guide(ignore=["E501"])
9
  report = style_guide.check_files(None, lines=code.splitlines())
10
 
11
+ # Auto-fix the code with autopep8
12
  fixed_code = autopep8.fix_code(code)
13
+
14
+ # Return fixed code and linting report as a tuple
15
+ lint_report = "\n".join(msg for msg in report.get_statistics())
16
+ return fixed_code, lint_report
17
 
18
+ def process_file(file) -> tuple[str, str]:
19
+ """Process the uploaded file and return the fixed code and linting report."""
20
+ # Read the file content and decode it to a string
21
  code = file.read().decode("utf-8")
22
+
23
+ # Lint the code and get the report
24
+ return lint_code(code)
25
+
26
+ def handle_input(code: str, file) -> tuple[str, str]:
27
+ """Handle both code inputs (text and file) and return fixed code and linting report."""
28
+ # Process file if provided, else process the pasted code
29
+ if file:
30
+ return process_file(file)
31
+ return lint_code(code)
32
 
33
+ # Create the Gradio interface
34
  interface = gr.Interface(
35
+ fn=handle_input,
36
  inputs=[
37
  gr.Textbox(lines=20, placeholder="Paste your Python code here...", label="Python Code"),
38
  gr.File(label="Upload a Python (.py) file", file_types=[".py"])
 
45
  description="Paste your Python code or upload a .py file to get a Flake8-compliant, linted version."
46
  )
47
 
48
+ # Launch the interface
49
+ interface.launch()