Spaces:
Runtime error
Runtime error
update
Browse files- app.py +5 -0
- py_code_analyzer/graph_analyzer.py +4 -9
- utils.py +12 -0
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
|
| 4 |
from py_code_analyzer import CodeFetcher, CodeImportsAnalyzer, ImportsGraphVisualizer
|
|
|
|
| 5 |
|
| 6 |
TITLE = "Python Code Analyzer"
|
| 7 |
st.set_page_config(page_title=TITLE, layout="wide")
|
|
@@ -26,18 +27,22 @@ clicked_ok_button = st.button("OK")
|
|
| 26 |
st.markdown("---")
|
| 27 |
|
| 28 |
|
|
|
|
| 29 |
def get_python_files(owner, repo, path, ref=""):
|
| 30 |
return CodeFetcher().get_python_files(owner, repo, path, ref=ref)
|
| 31 |
|
| 32 |
|
|
|
|
| 33 |
def generate_imports_graph(python_files):
|
| 34 |
return CodeImportsAnalyzer(python_files).analyze().generate_imports_graph()
|
| 35 |
|
| 36 |
|
|
|
|
| 37 |
def generate_graph_visualization_file(imports_graph):
|
| 38 |
ImportsGraphVisualizer().visualize(imports_graph)
|
| 39 |
|
| 40 |
|
|
|
|
| 41 |
def read_graph_visualization_file():
|
| 42 |
return open("nx.html", "r", encoding="utf-8").read()
|
| 43 |
|
|
|
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
|
| 4 |
from py_code_analyzer import CodeFetcher, CodeImportsAnalyzer, ImportsGraphVisualizer
|
| 5 |
+
from utils import time_function
|
| 6 |
|
| 7 |
TITLE = "Python Code Analyzer"
|
| 8 |
st.set_page_config(page_title=TITLE, layout="wide")
|
|
|
|
| 27 |
st.markdown("---")
|
| 28 |
|
| 29 |
|
| 30 |
+
@time_function
|
| 31 |
def get_python_files(owner, repo, path, ref=""):
|
| 32 |
return CodeFetcher().get_python_files(owner, repo, path, ref=ref)
|
| 33 |
|
| 34 |
|
| 35 |
+
@time_function
|
| 36 |
def generate_imports_graph(python_files):
|
| 37 |
return CodeImportsAnalyzer(python_files).analyze().generate_imports_graph()
|
| 38 |
|
| 39 |
|
| 40 |
+
@time_function
|
| 41 |
def generate_graph_visualization_file(imports_graph):
|
| 42 |
ImportsGraphVisualizer().visualize(imports_graph)
|
| 43 |
|
| 44 |
|
| 45 |
+
@time_function
|
| 46 |
def read_graph_visualization_file():
|
| 47 |
return open("nx.html", "r", encoding="utf-8").read()
|
| 48 |
|
py_code_analyzer/graph_analyzer.py
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
|
|
|
|
|
| 1 |
import networkx as nx
|
| 2 |
|
| 3 |
|
| 4 |
class GraphAnalyzer:
|
| 5 |
-
def __init__(self, is_directed: bool = False
|
| 6 |
-
if
|
| 7 |
-
self.graph = nx.Graph()
|
| 8 |
-
elif is_directed and not is_multi_edges:
|
| 9 |
-
self.graph = nx.DiGraph()
|
| 10 |
-
elif not is_directed and is_multi_edges:
|
| 11 |
-
self.graph = nx.MultiGraph()
|
| 12 |
-
else:
|
| 13 |
-
self.graph = nx.MultiDiGraph()
|
| 14 |
|
| 15 |
def add_node(self, node, **kwargs):
|
| 16 |
self.graph.add_node(node, **kwargs)
|
|
|
|
| 1 |
+
"""GraphAnalyzer uses some open source graph library for network analysis
|
| 2 |
+
"""
|
| 3 |
import networkx as nx
|
| 4 |
|
| 5 |
|
| 6 |
class GraphAnalyzer:
|
| 7 |
+
def __init__(self, is_directed: bool = False):
|
| 8 |
+
self.graph = nx.DiGraph() if is_directed else nx.Graph()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def add_node(self, node, **kwargs):
|
| 11 |
self.graph.add_node(node, **kwargs)
|
utils.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def time_function(f):
|
| 5 |
+
def wrapper(*args, **kwargs):
|
| 6 |
+
begin = time.time()
|
| 7 |
+
result = f(*args, **kwargs)
|
| 8 |
+
end = time.time()
|
| 9 |
+
print(f"function {f.__name__} took: {end - begin} seconds to execute.")
|
| 10 |
+
return result
|
| 11 |
+
|
| 12 |
+
return wrapper
|