Spaces:
Sleeping
Sleeping
File size: 8,958 Bytes
5abb0c5 b82e7a7 3d19c46 b82e7a7 5abb0c5 3d19c46 5abb0c5 3d19c46 b82e7a7 3d19c46 3145636 3d19c46 1f4b5de b82e7a7 3d19c46 5abb0c5 3d19c46 1f4b5de b82e7a7 5abb0c5 b82e7a7 3d19c46 b82e7a7 5abb0c5 5598c37 3d19c46 b82e7a7 5abb0c5 3d19c46 b82e7a7 5abb0c5 b82e7a7 5abb0c5 b82e7a7 5abb0c5 3d19c46 b82e7a7 3d19c46 b82e7a7 3d19c46 b82e7a7 3d19c46 b82e7a7 3d19c46 b82e7a7 5abb0c5 3d19c46 97db4d2 b82e7a7 3d19c46 b82e7a7 3d19c46 b82e7a7 0a5d964 b82e7a7 3d19c46 b82e7a7 5abb0c5 3d19c46 b82e7a7 3d19c46 b82e7a7 75b9a57 5abb0c5 2206288 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | import base64
import dash
from dash import html, dcc, dash_table
import dash_cytoscape as cyto
from dash.dependencies import Input, Output, State
import pandas as pd
from utility import parse_functions_from_files, get_reachable_functions, get_backtrace_functions, build_nodes_and_edges
from introduction import get_intro_markdown
app = dash.Dash(__name__, suppress_callback_exceptions=True)
server = app.server
app.layout = html.Div([
html.H2("Function Dependency Visualizer (AST-Based)", style={
"textAlign": "center",
"marginTop": "20px",
"fontSize": "28px",
"color": "#333"
}),
dcc.Store(id="parsed-functions-store"),
html.Div([
html.Div([
html.Label("Upload Files"),
dcc.Upload(
id="upload",
children=html.Div("📤 Drag and drop or click to upload Python files", style={
"border": "2px dashed #ccc",
"padding": "10px",
"textAlign": "center",
"cursor": "pointer",
"color": "#555",
"fontSize": "14px"
}),
multiple=True
)
], style={"width": "24%", "display": "inline-block", "verticalAlign": "top", "marginRight": "1%"}),
html.Div([
html.Label("Main Function"),
dcc.Dropdown(id="main-function", style={"width": "100%"})
], style={"width": "24%", "display": "inline-block", "verticalAlign": "top", "marginRight": "1%"}),
html.Div([
html.Label("Max Depth"),
dcc.Dropdown(
id="max-depth",
options=[{"label": str(i), "value": i} for i in range(1, 11)],
value=10,
clearable=False,
style={"width": "100%"}
)
], style={"width": "24%", "display": "inline-block", "verticalAlign": "top", "marginRight": "1%"}),
html.Div([
html.Label("Options"),
dcc.Checklist(
id="backtrace-toggle",
options=[{"label": "Backtrace Mode", "value": "backtrace"}],
value=[],
labelStyle={"display": "block", "fontWeight": "normal"}
),
dcc.Checklist(
id="function-detail-toggle",
options=[{"label": "Show Function-Level Detail in Summaray tab", "value": "show"}],
value=[],
labelStyle={"display": "block", "fontWeight": "normal"},
style={"marginTop": "5px"}
)
], style={"width": "24%", "display": "inline-block", "verticalAlign": "top"})
], style={
"backgroundColor": "#f9f9f9",
"padding": "15px",
"borderRadius": "10px",
"margin": "20px auto",
"width": "95%",
"boxShadow": "0 2px 6px rgba(0,0,0,0.1)"
}),
dcc.Tabs(id="tab-selector", value="intro", children=[
dcc.Tab(label="📘 Introduction", value="intro"),
dcc.Tab(label="📊 Graph Explorer", value="graph"),
dcc.Tab(label="📁 File Summary", value="summary")
], style={"margin": "0 20px"}),
html.Div(html.Div(id="intro-tab", style={"margin": "0 20px"}), id="intro-tab-container"),
html.Div(id="graph-tab-container", style={"margin": "0 20px"}),
html.Div(id="summary-tab-container", style={"margin": "0 20px"}),
html.Footer("Built by Tomas Larsson • MIT Licensed", style={
"textAlign": "center",
"fontSize": "14px",
"marginTop": "40px",
"marginBottom": "20px",
"color": "#888"
})
])
@app.callback(
Output("main-function", "options"),
Output("main-function", "value"),
Output("parsed-functions-store", "data"),
Input("upload", "contents"),
State("upload", "filename")
)
def handle_upload(contents, filenames):
print("=== handle_upload triggered ===")
if not contents:
return [], None, {}
uploaded_files = {}
for content, name in zip(contents, filenames):
_, content_string = content.split(",")
uploaded_files[name] = base64.b64decode(content_string).decode("utf-8")
parsed = parse_functions_from_files(uploaded_files)
# options = [{"label": fn, "value": fn} for fn in parsed if parsed[fn]["calls"]]
options = [{"label": fn, "value": fn} for fn in parsed]
return options, options[0]["value"] if options else None, parsed
@app.callback(
Output("intro-tab-container", "style"),
Output("graph-tab-container", "style"),
Output("summary-tab-container", "style"),
Input("tab-selector", "value")
)
def toggle_tabs(tab):
return (
{"display": "block"} if tab == "intro" else {"display": "none"},
{"display": "block"} if tab == "graph" else {"display": "none"},
{"display": "block"} if tab == "summary" else {"display": "none"}
)
@app.callback(Output("intro-tab", "children"), Input("tab-selector", "value"))
def show_intro(_):
return get_intro_markdown()
from collections import deque, defaultdict
@app.callback(
Output("graph-tab-container", "children"),
Input("main-function", "value"),
Input("backtrace-toggle", "value"),
Input("max-depth", "value"),
State("parsed-functions-store", "data")
)
def update_graph(main_func, backtrace_mode, max_depth, parsed):
if not parsed or not main_func:
return html.Div("Upload files and select a main function.")
graph = {k: v["calls"] for k, v in parsed.items()}
reachable = get_backtrace_functions(main_func, graph) if "backtrace" in backtrace_mode else get_reachable_functions(main_func, graph)
nodes, edges = build_nodes_and_edges(parsed, main_func, reachable, reverse="backtrace" in backtrace_mode, max_depth=max_depth)
return cyto.Cytoscape(
id="cytoscape-graph",
layout={"name": "preset"},
style={"width": "100%", "height": "600px"},
elements=nodes + edges,
stylesheet=[
{"selector": "node", "style": {
"label": "data(label)",
"text-wrap": "wrap",
"text-valign": "bottom",
"text-halign": "center"
}},
{"selector": ".main", "style": {
"background-color": "blue",
"line-color": "blue",
"color": "black",
"label": "data(label)",
"text-wrap": "wrap",
"text-valign": "bottom",
"text-halign": "center"
}},
{"selector": "edge", "style": {
"label": "data(label)",
"curve-style": "bezier",
"target-arrow-shape": "triangle",
"target-arrow-color": "#888",
"line-color": "#888",
"arrow-scale": 2,
"font-size": "14px",
"text-margin-y": -10,
"text-margin-x": 10,
}}
],
userZoomingEnabled=True,
userPanningEnabled=True,
minZoom=0.2,
maxZoom=2,
wheelSensitivity=0.1
)
@app.callback(
Output("summary-tab-container", "children"),
Input("parsed-functions-store", "data"),
Input("function-detail-toggle", "value")
)
def update_summary(parsed, toggle):
if not parsed:
return html.Div("No summary available.")
reverse_calls = {k: [] for k in parsed}
for caller, meta in parsed.items():
for callee in meta["calls"]:
reverse_calls[callee].append(caller)
if "show" in toggle:
df = pd.DataFrame([{
"Function": fn,
"File": parsed[fn]["filename"],
"Arguments": ", ".join(parsed[fn]["args"]),
"Returns": ", ".join(parsed[fn]["returns"]),
"Reads State": ", ".join(parsed[fn]["reads_state"]),
"Writes State": ", ".join(parsed[fn]["writes_state"]),
"Called By": len(reverse_calls[fn])
} for fn in parsed])
else:
file_summary = {}
for func, meta in parsed.items():
fname = meta["filename"]
file_summary.setdefault(fname, {"Total": 0, "Calls Others": 0, "Called By Others": 0, "Unused": 0})
file_summary[fname]["Total"] += 1
if meta["calls"]: file_summary[fname]["Calls Others"] += 1
if reverse_calls[func]: file_summary[fname]["Called By Others"] += 1
if not meta["calls"] and not reverse_calls[func]: file_summary[fname]["Unused"] += 1
df = pd.DataFrame([{"File": f, **stats} for f, stats in file_summary.items()])
return dash_table.DataTable(
data=df.to_dict("records"),
columns=[{"name": c, "id": c} for c in df.columns],
style_table={"overflowX": "auto"},
style_cell={"whiteSpace": "normal", "textAlign": "left", "padding": "5px", "maxWidth": 300}
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) |