File size: 3,909 Bytes
ad1dcd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<html><head><base href="https://websim.ai/python-bug-buster"><title>Python Bug Buster: Detect and Fix Bugs in Your Code</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/python/python.min.js"></script>
</head>
<body class="bg-gray-100 min-h-screen font-sans">
  <header class="bg-purple-600 text-white py-6">
    <div class="container mx-auto px-4">
      <h1 class="text-3xl font-bold">Python Bug Buster</h1>
      <p class="mt-2">Detect and Fix Bugs in Your Python Code</p>
    </div>
  </header>

  <main class="container mx-auto px-4 py-8">
    <div class="bg-white rounded-lg shadow-md p-6">
      <h2 class="text-2xl font-semibold mb-4">Code Editor</h2>
      <textarea id="code-editor" class="w-full h-64 font-mono text-sm"></textarea>
      <button id="analyze-button" class="mt-4 py-2 px-4 bg-purple-600 text-white rounded-md hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:ring-offset-2">
        Analyze and Fix Bugs
      </button>
    </div>

    <div id="result-container" class="mt-8 bg-white rounded-lg shadow-md p-6 hidden">
      <h2 class="text-2xl font-semibold mb-4">Analysis Result</h2>
      <div id="bug-list" class="mb-4"></div>
      <h3 class="text-xl font-semibold mb-2">Fixed Code:</h3>
      <pre id="fixed-code" class="bg-gray-100 p-4 rounded-md overflow-x-auto"></pre>
    </div>
  </main>

  <script>
    let editor;

    document.addEventListener('DOMContentLoaded', (event) => {
      editor = CodeMirror.fromTextArea(document.getElementById("code-editor"), {
        mode: "python",
        lineNumbers: true,
        theme: "default"
      });

      // Sample code with bugs
      editor.setValue(`def calculate_average(numbers):
    total = 0
    for num in numbers
        total += num
    return total / len(numbers)

def main():
    scores = [85, 92, 78, 90, 88]
    average = calculate_average(scores)
    print(f"The average score is: {average}"")

if __name__ == "__main__":
    main()`);
    });

    document.getElementById('analyze-button').addEventListener('click', function() {
      const code = editor.getValue();
      analyzeAndFixCode(code);
    });

    function analyzeAndFixCode(code) {
      // Simulate bug detection and fixing
      setTimeout(() => {
        const bugList = [
          "Syntax error: Missing colon (:) after 'for' loop declaration",
          "Syntax error: Extra quotation mark in print statement",
          "Potential division by zero if 'numbers' list is empty"
        ];

        const fixedCode = `def calculate_average(numbers):
    if not numbers:
        return 0  # Return 0 for empty list to avoid division by zero
    total = 0
    for num in numbers:  # Added missing colon
        total += num
    return total / len(numbers)

def main():
    scores = [85, 92, 78, 90, 88]
    average = calculate_average(scores)
    print(f"The average score is: {average}")  # Removed extra quotation mark

if __name__ == "__main__":
    main()`;

        displayResults(bugList, fixedCode);
      }, 1500);
    }

    function displayResults(bugList, fixedCode) {
      const resultContainer = document.getElementById('result-container');
      const bugListElement = document.getElementById('bug-list');
      const fixedCodeElement = document.getElementById('fixed-code');

      resultContainer.classList.remove('hidden');

      bugListElement.innerHTML = '<h3 class="text-xl font-semibold mb-2">Detected Bugs:</h3><ul class="list-disc pl-5">' +
        bugList.map(bug => `<li>${bug}</li>`).join('') +
        '</ul>';

      fixedCodeElement.textContent = fixedCode;
    }
  </script>
</body></html>