Canstralian commited on
Commit
ae0dabc
·
verified ·
1 Parent(s): c6a3dfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -1,28 +1,44 @@
1
  import gradio as gr
2
- import flake8
3
  import autopep8
 
 
4
 
5
  def lint_code(code):
6
- # Initialize Flake8 style guide
7
- style_guide = flake8.get_style_guide()
8
- # Create a checker for the provided code
9
- checker = style_guide.check_files([code])
10
- # Collect linting issues
11
- issues = [f"{issue.filename}:{issue.line_number}: {issue.text}" for issue in checker.get_statistics('')]
12
- # Format the code using AutoPEP8
13
- formatted_code = autopep8.fix_code(code)
14
- return "\n".join(issues), formatted_code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def main():
17
  with gr.Interface(
18
  fn=lint_code,
19
- inputs="text",
20
- outputs=["text", "text"],
21
  title="PyLintPro",
22
  description="A Gradio app that helps users improve Python code to meet Flake8 and PEP 8 standards.",
23
- live=True
24
  ) as demo:
25
  demo.launch()
26
 
27
  if __name__ == "__main__":
28
- main()
 
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
 
43
  if __name__ == "__main__":
44
+ main()