Canstralian commited on
Commit
eb73a33
·
verified ·
1 Parent(s): 5b1cddb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -18
app.py CHANGED
@@ -1,42 +1,97 @@
1
  import gradio as gr
2
  import flake8.main.application
3
  import autopep8
 
 
 
4
  import tempfile
5
  import os
 
 
6
 
7
- def lint_code(code):
8
- # Create a temporary file to store the code
 
 
 
 
 
 
 
9
  with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file:
10
  temp_file.write(code)
11
  temp_file_path = temp_file.name
12
 
13
  try:
14
- # Run Flake8
15
- app = flake8.main.application.Application()
16
- app.initialize(['--format=default', temp_file_path])
17
- app.run_checks()
18
- app.generate_reports()
19
- flake8_output = app.results_manager.stdout.getvalue().strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Format the code using AutoPEP8
22
- formatted_code = autopep8.fix_code(code)
23
 
24
- return flake8_output, formatted_code
 
 
 
 
 
 
 
 
25
 
26
  except Exception as e:
27
- return f"An error occurred: {str(e)}", code # Return original code on error
28
 
29
  finally:
30
- # Clean up the temporary file
31
  os.remove(temp_file_path)
32
 
33
  def main():
34
  with gr.Interface(
35
- fn=lint_code,
36
- inputs=gr.Textbox(lines=10, placeholder="Enter your Python code here..."),
37
- outputs=[gr.Textbox(label="Flake8 Output"), gr.Code(label="Formatted Code", language="python")],
38
- title="PyLintPro",
39
- description="A Gradio app that helps users improve Python code to meet Flake8 and PEP 8 standards.",
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  ) as demo:
41
  demo.launch()
42
 
 
1
  import gradio as gr
2
  import flake8.main.application
3
  import autopep8
4
+ import pylint.lint
5
+ import isort
6
+ import black
7
  import tempfile
8
  import os
9
+ import difflib
10
+ import io
11
 
12
+ def lint_and_format(code, enable_flake8, enable_pylint, enable_isort, enable_black):
13
+ flake8_output = ""
14
+ pylint_output = ""
15
+ isort_output = ""
16
+ formatted_code = code
17
+ diff_output = ""
18
+ black_output = ""
19
+
20
+ # Temporary file for linters
21
  with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py') as temp_file:
22
  temp_file.write(code)
23
  temp_file_path = temp_file.name
24
 
25
  try:
26
+ # Flake8
27
+ if enable_flake8:
28
+ app = flake8.main.application.Application()
29
+ app.initialize(['--format=default', temp_file_path])
30
+ app.run_checks()
31
+ app.generate_reports()
32
+ flake8_output = app.results_manager.stdout.getvalue().strip()
33
+
34
+ # Pylint
35
+ if enable_pylint:
36
+ pylint_output_stream = io.StringIO()
37
+ pylint_runner = pylint.lint.Run([temp_file_path], do_exit=False, output_stream=pylint_output_stream)
38
+ pylint_output = pylint_output_stream.getvalue().strip()
39
+
40
+ # isort
41
+ if enable_isort:
42
+ isort_output = isort.file(temp_file_path, show_diff=True).strip()
43
+
44
+ # Black
45
+ if enable_black:
46
+ try:
47
+ formatted_code = black.format_file_in_place(
48
+ src=temp_file_path,
49
+ fast=False,
50
+ mode=black.FileMode(),
51
+ write_back=black.WriteBack.YES,
52
+ )
53
+ with open(temp_file_path, "r") as f:
54
+ formatted_code = f.read()
55
 
56
+ except black.NothingChanged:
57
+ pass # black did not change anything
58
 
59
+ # AutoPEP8 (if Black is not enabled)
60
+ if not enable_black:
61
+ formatted_code = autopep8.fix_code(formatted_code)
62
+
63
+ # Diff
64
+ diff = difflib.unified_diff(code.splitlines(keepends=True), formatted_code.splitlines(keepends=True), fromfile="original.py", tofile="formatted.py")
65
+ diff_output = "".join(diff)
66
+
67
+ return flake8_output, pylint_output, isort_output, formatted_code, diff_output
68
 
69
  except Exception as e:
70
+ return f"An error occurred: {str(e)}", "", "", code, ""
71
 
72
  finally:
 
73
  os.remove(temp_file_path)
74
 
75
  def main():
76
  with gr.Interface(
77
+ fn=lint_and_format,
78
+ inputs=[
79
+ gr.Code(label="Python Code", language="python", lines=10),
80
+ gr.Checkbox(label="Enable Flake8", value=True),
81
+ gr.Checkbox(label="Enable Pylint", value=False),
82
+ gr.Checkbox(label="Enable isort", value=True),
83
+ gr.Checkbox(label="Enable Black", value=True),
84
+
85
+ ],
86
+ outputs=[
87
+ gr.Textbox(label="Flake8 Output"),
88
+ gr.Textbox(label="Pylint Output"),
89
+ gr.Textbox(label="isort Output"),
90
+ gr.Code(label="Formatted Code", language="python"),
91
+ gr.Textbox(label="Diff"),
92
+ ],
93
+ title="Python Code Linter and Formatter",
94
+ description="Paste your Python code, and this tool will lint and format it using Flake8, Pylint, isort and Black/AutoPEP8.",
95
  ) as demo:
96
  demo.launch()
97