arif670 commited on
Commit
ffdc606
·
verified ·
1 Parent(s): cb37200

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -20
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from graphviz import Digraph
4
- import base64
5
  from io import StringIO
 
6
 
7
  # Custom CSS for professional UI
8
  st.markdown("""
@@ -10,12 +10,14 @@ st.markdown("""
10
  /* Main container */
11
  .stApp {
12
  background-color: #f5f5f5;
 
13
  }
14
 
15
  /* Sidebar */
16
  .stSidebar {
17
  background-color: #2c3e50;
18
  color: white;
 
19
  }
20
 
21
  /* Buttons */
@@ -52,28 +54,104 @@ st.markdown("""
52
  background-color: #4CAF50;
53
  border-radius: 8px;
54
  }
 
 
 
 
 
 
 
 
55
  </style>
56
  """, unsafe_allow_html=True)
57
 
58
  # Function to create organization chart
59
  def create_org_chart(df, title):
60
- dot = Digraph(comment='Organization Chart')
61
- dot.attr(rankdir='TB', labelloc='t', label=title, fontsize='20', fontname='Arial')
 
 
 
 
62
 
63
- # Add nodes and edges
 
64
  for _, row in df.iterrows():
65
  parent = row['Parent']
66
  child = row['Child']
67
-
68
- # Add parent node if not already added
69
- if parent not in dot.body:
70
- dot.node(parent, shape='box', style='filled', fillcolor='#4CAF50', fontname='Arial', fontcolor='white')
71
-
72
- # Add child node and edge
73
- dot.node(child, shape='box', style='filled', fillcolor='#4CAF50', fontname='Arial', fontcolor='white')
74
- dot.edge(parent, child)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- return dot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # Function to download CSV template
79
  def download_csv_template():
@@ -131,10 +209,8 @@ CFO,Accounting Manager"""
131
  # Generate chart
132
  if st.button("Generate Organization Chart"):
133
  if not edited_df.empty:
134
- dot = create_org_chart(edited_df, chart_title)
135
-
136
- # Display the chart
137
- st.graphviz_chart(dot)
138
 
139
  # PDF download options
140
  st.markdown("### Download Options")
@@ -146,10 +222,12 @@ CFO,Accounting Manager"""
146
 
147
  # Convert to PDF
148
  if st.button("Download as PDF"):
149
- pdf_bytes = dot.pipe(format='pdf')
 
 
150
  st.download_button(
151
  label="Download PDF",
152
- data=pdf_bytes,
153
  file_name="org_chart.pdf",
154
  mime="application/pdf"
155
  )
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.graph_objects as go
 
4
  from io import StringIO
5
+ import base64
6
 
7
  # Custom CSS for professional UI
8
  st.markdown("""
 
10
  /* Main container */
11
  .stApp {
12
  background-color: #f5f5f5;
13
+ font-family: 'Arial', sans-serif;
14
  }
15
 
16
  /* Sidebar */
17
  .stSidebar {
18
  background-color: #2c3e50;
19
  color: white;
20
+ padding: 20px;
21
  }
22
 
23
  /* Buttons */
 
54
  background-color: #4CAF50;
55
  border-radius: 8px;
56
  }
57
+
58
+ /* Organization chart container */
59
+ .org-chart-container {
60
+ background-color: white;
61
+ border-radius: 12px;
62
+ padding: 20px;
63
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
64
+ }
65
  </style>
66
  """, unsafe_allow_html=True)
67
 
68
  # Function to create organization chart
69
  def create_org_chart(df, title):
70
+ positions = {}
71
+ edge_x = []
72
+ edge_y = []
73
+ node_x = []
74
+ node_y = []
75
+ node_text = []
76
 
77
+ # Create hierarchical positions
78
+ levels = {}
79
  for _, row in df.iterrows():
80
  parent = row['Parent']
81
  child = row['Child']
82
+ if parent not in levels:
83
+ levels[parent] = 0
84
+ if child not in levels:
85
+ levels[child] = levels[parent] + 1
86
+
87
+ # Assign coordinates
88
+ max_level = max(levels.values())
89
+ level_positions = {level: [] for level in range(max_level + 1)}
90
+
91
+ for node, level in levels.items():
92
+ level_positions[level].append(node)
93
+
94
+ for level, nodes in level_positions.items():
95
+ y_pos = max_level - level
96
+ x_positions = [i - (len(nodes)-1)/2 for i in range(len(nodes))]
97
+ for node, x_pos in zip(nodes, x_positions):
98
+ positions[node] = (x_pos, y_pos)
99
+
100
+ # Create edges
101
+ for _, row in df.iterrows():
102
+ parent = row['Parent']
103
+ child = row['Child']
104
+ x0, y0 = positions[parent]
105
+ x1, y1 = positions[child]
106
+ edge_x.extend([x0, x1, None])
107
+ edge_y.extend([y0, y1, None])
108
+
109
+ # Create nodes
110
+ for node, (x, y) in positions.items():
111
+ node_x.append(x)
112
+ node_y.append(y)
113
+ node_text.append(node)
114
+
115
+ # Create 3D effect
116
+ edge_trace = go.Scatter(
117
+ x=edge_x, y=edge_y,
118
+ line=dict(width=2, color='#888'),
119
+ hoverinfo='none',
120
+ mode='lines'
121
+ )
122
 
123
+ node_trace = go.Scatter(
124
+ x=node_x, y=node_y,
125
+ mode='markers+text',
126
+ text=node_text,
127
+ textposition="middle center",
128
+ marker=dict(
129
+ symbol='square',
130
+ size=[100] * len(node_x), # Scalable size
131
+ color='#4CAF50',
132
+ line=dict(width=2, color='DarkSlateGrey'),
133
+ opacity=0.8
134
+ ),
135
+ hoverinfo='text',
136
+ textfont=dict(size=14, color='white'),
137
+ texttemplate='%{text}<br>', # Wrap text
138
+ )
139
+
140
+ fig = go.Figure(data=[edge_trace, node_trace],
141
+ layout=go.Layout(
142
+ title=f'<b>{title}</b><br>',
143
+ titlefont_size=20,
144
+ showlegend=False,
145
+ hovermode='closest',
146
+ margin=dict(b=20, l=5, r=5, t=40),
147
+ xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
148
+ yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
149
+ plot_bgcolor='white',
150
+ paper_bgcolor='white'
151
+ )
152
+ )
153
+
154
+ return fig
155
 
156
  # Function to download CSV template
157
  def download_csv_template():
 
209
  # Generate chart
210
  if st.button("Generate Organization Chart"):
211
  if not edited_df.empty:
212
+ fig = create_org_chart(edited_df, chart_title)
213
+ st.plotly_chart(fig, use_container_width=True)
 
 
214
 
215
  # PDF download options
216
  st.markdown("### Download Options")
 
222
 
223
  # Convert to PDF
224
  if st.button("Download as PDF"):
225
+ img_bytes = fig.to_image(format="pdf",
226
+ width=800 if orientation == "Portrait" else 1200,
227
+ height=1200 if orientation == "Portrait" else 800)
228
  st.download_button(
229
  label="Download PDF",
230
+ data=img_bytes,
231
  file_name="org_chart.pdf",
232
  mime="application/pdf"
233
  )