Devadarshan commited on
Commit
1d144f7
·
verified ·
1 Parent(s): c5d03db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -38
app.py CHANGED
@@ -4,32 +4,35 @@ from radon.complexity import cc_visit
4
  import dash_bootstrap_components as dbc
5
 
6
  app = Dash(__name__, external_stylesheets=[dbc.themes.CYBORG])
 
7
 
8
  def analyze_code(code):
9
 
10
  if not code:
11
  return 0,"Paste code to analyze",0
12
 
13
- results = cc_visit(code)
 
 
 
 
14
 
15
- complexity = sum([r.complexity for r in results])
16
-
17
- if complexity==0:
18
- complexity=1
19
 
20
  quality = max(100-(complexity*4),40)
21
 
22
- if complexity>15:
23
- suggestion="Code is complex. Try reducing nested loops."
24
- elif complexity>8:
25
- suggestion="Moderate complexity. Consider simplifying logic."
26
  else:
27
- suggestion="Code quality looks good."
28
 
29
- return quality,suggestion,complexity
30
 
31
 
32
- def gauge(score):
33
 
34
  fig = go.Figure(go.Indicator(
35
 
@@ -42,6 +45,11 @@ def gauge(score):
42
  gauge={
43
  'axis':{'range':[0,100]},
44
  'bar':{'color':"lime"},
 
 
 
 
 
45
  }
46
 
47
  ))
@@ -53,8 +61,8 @@ def gauge(score):
53
 
54
  app.layout = dbc.Container([
55
 
56
- html.H1("AI Code Quality Analyzer",
57
- style={'textAlign':'center'}),
58
 
59
  dbc.Row([
60
 
@@ -64,66 +72,85 @@ html.H4("Enter Python Code"),
64
 
65
  dcc.Textarea(
66
 
67
- id="code",
 
 
68
 
69
- style={'width':'100%','height':300}
 
 
 
 
 
 
70
 
71
  ),
72
 
73
  html.Br(),
74
 
75
- dbc.Button("Analyze",
76
- id="btn",
77
- color="primary")
 
 
 
78
 
79
- ],width=6),
80
 
81
  dbc.Col([
82
 
83
- html.H4("Results"),
84
 
85
- html.Div(id="suggestion"),
86
 
87
- dcc.Graph(id="score"),
 
 
 
 
88
 
89
- html.Div(id="complexity")
90
 
91
- ],width=6)
92
 
93
  ])
94
 
95
- ],fluid=True)
96
 
 
97
 
98
- @app.callback(
99
 
100
- Output("suggestion","children"),
101
 
102
- Output("score","figure"),
103
 
104
- Output("complexity","children"),
105
 
106
- Input("btn","n_clicks"),
 
 
107
 
108
- Input("code","value")
 
109
 
110
  )
111
 
112
- def update(n,code):
113
 
114
- quality,suggestion,complexity = analyze_code(code)
115
 
116
- fig=gauge(quality)
117
 
118
  return (
119
 
120
- "AI Suggestion: "+suggestion,
121
 
122
  fig,
123
 
124
- "Complexity Score: "+str(complexity)
125
 
126
  )
127
 
128
 
129
- app.run(host="0.0.0.0",port=7860)
 
 
4
  import dash_bootstrap_components as dbc
5
 
6
  app = Dash(__name__, external_stylesheets=[dbc.themes.CYBORG])
7
+ server = app.server
8
 
9
  def analyze_code(code):
10
 
11
  if not code:
12
  return 0,"Paste code to analyze",0
13
 
14
+ try:
15
+ results = cc_visit(code)
16
+ complexity = sum([r.complexity for r in results])
17
+ except:
18
+ complexity = 1
19
 
20
+ if complexity == 0:
21
+ complexity = 1
 
 
22
 
23
  quality = max(100-(complexity*4),40)
24
 
25
+ if complexity > 15:
26
+ suggestion = "Code is highly complex. Reduce nested loops and split functions."
27
+ elif complexity > 8:
28
+ suggestion = "Moderate complexity. Try simplifying logic."
29
  else:
30
+ suggestion = "Code quality looks good."
31
 
32
+ return quality, suggestion, complexity
33
 
34
 
35
+ def create_gauge(score):
36
 
37
  fig = go.Figure(go.Indicator(
38
 
 
45
  gauge={
46
  'axis':{'range':[0,100]},
47
  'bar':{'color':"lime"},
48
+ 'steps':[
49
+ {'range':[0,50],'color':"red"},
50
+ {'range':[50,75],'color':"orange"},
51
+ {'range':[75,100],'color':"green"}
52
+ ]
53
  }
54
 
55
  ))
 
61
 
62
  app.layout = dbc.Container([
63
 
64
+ html.H1("AI Code Quality Analyzer Dashboard",
65
+ style={'textAlign':'center','marginBottom':'30px'}),
66
 
67
  dbc.Row([
68
 
 
72
 
73
  dcc.Textarea(
74
 
75
+ id="code_input",
76
+
77
+ placeholder="Paste your Python code here...",
78
 
79
+ style={
80
+ 'width':'100%',
81
+ 'height':'350px',
82
+ 'backgroundColor':'#111',
83
+ 'color':'white',
84
+ 'border':'1px solid gray'
85
+ }
86
 
87
  ),
88
 
89
  html.Br(),
90
 
91
+ dbc.Button(
92
+ "Analyze Code",
93
+ id="analyze_btn",
94
+ color="primary",
95
+ style={'width':'100%'}
96
+ )
97
 
98
+ ], width=6),
99
 
100
  dbc.Col([
101
 
102
+ html.H4("Analysis Results"),
103
 
104
+ dbc.Card([
105
 
106
+ dbc.CardBody([
107
+
108
+ html.H5(id="suggestion"),
109
+
110
+ html.Br(),
111
 
112
+ dcc.Graph(id="quality_graph"),
113
 
114
+ html.H5(id="complexity_score")
115
 
116
  ])
117
 
118
+ ])
119
 
120
+ ], width=6)
121
 
122
+ ])
123
 
124
+ ], fluid=True)
125
 
 
126
 
127
+ @app.callback(
128
 
129
+ Output("suggestion","children"),
130
+ Output("quality_graph","figure"),
131
+ Output("complexity_score","children"),
132
 
133
+ Input("analyze_btn","n_clicks"),
134
+ Input("code_input","value")
135
 
136
  )
137
 
138
+ def update_output(n_clicks, code):
139
 
140
+ quality, suggestion, complexity = analyze_code(code)
141
 
142
+ fig = create_gauge(quality)
143
 
144
  return (
145
 
146
+ "AI Suggestion: " + suggestion,
147
 
148
  fig,
149
 
150
+ "Code Complexity Score: " + str(complexity)
151
 
152
  )
153
 
154
 
155
+ if __name__ == "__main__":
156
+ app.run(host="0.0.0.0", port=7860)