Skip to content

Commit

Permalink
feat: add gui
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspec committed Oct 12, 2023
1 parent b671c21 commit dc2ff35
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
103 changes: 103 additions & 0 deletions moduler/gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from moduler import Struct
import plotly.graph_objects as go

def draw_treemap(struct: Struct):
ids, labels, parents, texts = extract_tree_ingredients(struct)

fig = go.Figure(go.Treemap(
labels=labels,
parents=parents,
# values=values,
ids=ids,
text=texts,
# text=values,
root_color="lightgrey",
# hoverinfo="label+text",
# hovertemplate="<b>%{label}</b><br>%{hovertext}",
texttemplate="<b>%{label}</b><br>%{text}",
hovertemplate="<b>%{label}</b><br>%{text}<extra></extra>",
hoverinfo="text",
# marker=dict(cornerradius=5)
))

fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
# fig.update_traces(marker=dict(cornerradius=5))
fig.show()


"""
## Extract tree ingredients for treemap visualization
"""

def extract_tree_ingredients(root: Struct):
labels = []
parents = []
texts = []
ids = []
add_ingredients_to_lists(root, root.name, 0, labels, parents, texts, ids)
return ids, labels, parents, texts

def add_ingredients_to_lists(root: Struct, root_path: str, root_idx: int, labels, parents, texts, ids):
n_sections = 0
for child in root.children:
if child.struct_type == "comment":
texts[root_idx] += child.obj
continue
labels.append(child.name)
parents.append(root_path)
if child.struct_type in "section":
n_sections += 1
child_name = child.struct_type + " " + str(n_sections) + ". " + child.name
else:
child_name = child.struct_type + " " + child.name
child_path = root_path + child_name + "/"
ids.append(child_path)
if child.struct_type == "section":
texts.append(child.name)
add_ingredients_to_lists(child, child_path, len(texts)-1, labels, parents, texts, ids)
elif child.struct_type == "module":
texts.append("")
add_ingredients_to_lists(child, child_path, len(texts)-1, labels, parents, texts, ids)
elif child.struct_type == "class":
texts.append("")
add_ingredients_to_lists(child, child_path, len(texts)-1, labels, parents, texts, ids)
elif child.struct_type == "function":
texts.append("")
add_ingredients_to_lists(child, child_path, len(texts)-1, labels, parents, texts, ids)



"""
## Hypenate texts
"""

from hyphen import Hyphenator
from hyphen.textwrap2 import fill

h_en = Hyphenator('en_US')


def hypenate_texts(texts: str, line_width=40):
"""
Hypenate texts. Add <br> to the end of each line.
:param texts: The texts to be hypenated
:param line_width: The width of each line
:return: The hypenated texts
"""
if "\\" in texts:
hyphenator = False
else:
hyphenator = h_en
try:
texts = fill(texts, width=line_width, use_hyphenator=hyphenator)
except:
texts = fill(texts, width=line_width, use_hyphenator=False)
texts = texts.replace("\n", "<br>")
return texts


if __name__ == '__main__':
from moduler.core import build_module_tree
import moduler
struct = build_module_tree(moduler)
draw_treemap(struct)
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ build-backend = "setuptools.build_meta"

[project]
name = "Moduler"
description = "Intelligence-boosting annotation system"
description = "Annotation system for boosting intelligence"
version = "0.1.0"
authors = [
{ name = "Zijian Zhang" }
]

dependencies = []
dependencies = [
"pyyaml~=6.0.1",
]

[project.optional-dependencies]
dev = [
"pytest"
"pytest",
"plotly==5.16.1",
"pyhyphen==4.0.3",
]

[tool.setuptools]
Expand Down

0 comments on commit dc2ff35

Please sign in to comment.