-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (62 loc) · 2.11 KB
/
app.py
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
import pyperclip
import streamlit as st
from streamlit_mermaid import st_mermaid
from streamlit_monaco import st_monaco
from functions.adj_matrix import create_adjacency_matrix, parse_edges
from functions.eq_generation import generate_from_matrix, generate_from_mermaid
if not 'user_code' in st.session_state.keys():
st.session_state['user_code'] = None
if not 'adj_matrix' in st.session_state.keys():
st.session_state['adj_matrix'] = None
st.set_page_config(
layout="wide",
page_icon="⚙️"
)
st.title("Mermaid construction")
def draw(inp_code):
st_mermaid(inp_code, height="250px", width="400px")
def latex_copy(inp_code):
pyperclip.copy(inp_code)
st.toast('Latex code was succsessfully copied to clipboard!')
col1, col2 = st.columns([.4, .6])
with col1:
mermaid_code = """graph LR;
a-- 0.5 -->b;
a-- 0.5 -->c;
b-- 0.5 -->d;
b-- 0.5 -->e;
c-- 0.5 -->e;
c-- 0.5 -->d;
d-- 0.5 -->f;
d-- 0.5 -->g;
e-- 0.5 -->g;
e-- 0.5 -->f;
"""
user_code = st_monaco(value=mermaid_code, height="300px", language="markdown")
st.session_state['user_code'] = user_code
with col2:
draw(user_code)
source = st.radio('Source', ['Mermaid code', 'Adjancecy matrix'])
gen_matrix_btn = st.button('Generate matrix')
lcol, rcol = st.columns(2)
if gen_matrix_btn:
if source == 'Mermaid code':
result = generate_from_mermaid(user_code)
lcol.code(result)
rcol.latex(result)
if source == 'Adjancecy matrix':
edges = parse_edges(user_code)
frame, verticies = create_adjacency_matrix(edges)
frame.columns = verticies
frame.index = verticies
ltx_code = generate_from_matrix(adj_matrix=frame)
json_tab, matrix_tab, latex_tab = lcol.tabs(['JSON', 'Matrix', 'LaTeX'])
with latex_tab:
st.code(ltx_code)
st.button('Copy to clipboard', on_click=latex_copy, args=(ltx_code, ))
with json_tab:
st.session_state['adj_matrix'] = frame.values
st.json(edges, expanded=False)
with matrix_tab:
st.table(frame)
rcol.latex(ltx_code)