cyyeh/py-code-analyzer
0
1import asyncio2import os3 4import streamlit as st5import streamlit.components.v1 as components6 7from py_code_analyzer import CodeFetcher, CodeImportsAnalyzer, ImportsGraphVisualizer8from utils import conditonal_decorator, time_function9 10DEV = int(os.environ.get("DEV", "1"))11 12TITLE = "Python Code Analyzer"13st.set_page_config(page_title=TITLE, layout="wide")14st.title(TITLE)15st.markdown(16 "The main purpose of the app is to allow Python developers navigate Python code base much "17 + "easier by showing dependencies among files included in the directory with better visualization."18)19st.markdown(20 "**Checkout the source code [here](https://github.com/cyyeh/py-code-analyzer)**"21)22 23owner = st.text_input("Fill in the GitHub username", value="cyyeh")24repo = st.text_input("Fill in the GitHib repository", value="py-code-analyzer")25tree_sha = st.text_input(26 "Fill in SHA", value="2f387d0adea72a7b4c99a5e8fc3e4fd83b5469b8"27)28show_graph_visualization = st.checkbox(29 "Show graph visualization",30 value=True,31 help="If the graph is large, then consider uncheck the checkbox. "32 "For example, the result graph of fetching TensorFlow repo would be large.",33)34clicked_ok_button = st.button("OK", disabled=not owner or not repo or not tree_sha)35st.markdown("---")36 37 38@st.cache39@conditonal_decorator(time_function, DEV)40def get_python_files(owner: str, repo: str, tree_sha: str):41 return CodeFetcher().get_python_files(owner, repo, tree_sha)42 43 44@conditonal_decorator(time_function, DEV)45def parse_python_files(analyzer: CodeImportsAnalyzer):46 asyncio.run(analyzer.parse_python_files())47 48 49@conditonal_decorator(time_function, DEV)50def generate_imports_graph(analyzer: CodeImportsAnalyzer):51 return analyzer.generate_imports_graph()52 53 54@conditonal_decorator(time_function, DEV)55def generate_graph_visualization_file(imports_graph, heading: str):56 ImportsGraphVisualizer().visualize(imports_graph, heading=heading)57 58 59@conditonal_decorator(time_function, DEV)60def read_graph_visualization_file():61 return open("nx.html", "r", encoding="utf-8").read()62 63 64if clicked_ok_button and owner and repo:65 with st.spinner("Getting python files..."):66 python_files = get_python_files(owner, repo, tree_sha)67 68 analyzer = CodeImportsAnalyzer(python_files)69 with st.spinner("Parsing python files..."):70 parse_python_files(analyzer)71 72 with st.spinner("Generating imports graph..."):73 imports_graph = generate_imports_graph(analyzer)74 75 with st.spinner("Generating graph visualization file..."):76 generate_graph_visualization_file(imports_graph, f"{owner}/{repo}")77 graph_visualization_file = read_graph_visualization_file()78 st.download_button(79 "Download the result file",80 graph_visualization_file,81 file_name="result.html",82 mime="text/html",83 )84 85 if show_graph_visualization:86 with st.spinner("Showing the graph..."):87 components.html(graph_visualization_file, height=600, scrolling=True)88 