arif670 commited on
Commit
5092326
·
verified ·
1 Parent(s): d8fed7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -83
app.py CHANGED
@@ -1,8 +1,8 @@
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("""
@@ -57,86 +57,23 @@ st.markdown("""
57
 
58
  # Function to create organization chart
59
  def create_org_chart(df, title):
60
- positions = {}
61
- edge_x = []
62
- edge_y = []
63
- node_x = []
64
- node_y = []
65
- node_text = []
66
-
67
- # Create hierarchical positions
68
- levels = {}
69
- for _, row in df.iterrows():
70
- parent = row['Parent']
71
- child = row['Child']
72
- if parent not in levels:
73
- levels[parent] = 0
74
- if child not in levels:
75
- levels[child] = levels[parent] + 1
76
-
77
- # Assign coordinates
78
- max_level = max(levels.values())
79
- level_positions = {level: [] for level in range(max_level + 1)}
80
-
81
- for node, level in levels.items():
82
- level_positions[level].append(node)
83
-
84
- for level, nodes in level_positions.items():
85
- y_pos = max_level - level
86
- x_positions = [i - (len(nodes)-1)/2 for i in range(len(nodes))]
87
- for node, x_pos in zip(nodes, x_positions):
88
- positions[node] = (x_pos, y_pos)
89
 
90
- # Create edges
91
  for _, row in df.iterrows():
92
  parent = row['Parent']
93
  child = row['Child']
94
- x0, y0 = positions[parent]
95
- x1, y1 = positions[child]
96
- edge_x.extend([x0, x1, None])
97
- edge_y.extend([y0, y1, None])
98
-
99
- # Create nodes
100
- for node, (x, y) in positions.items():
101
- node_x.append(x)
102
- node_y.append(y)
103
- node_text.append(node)
104
-
105
- # Create 3D effect
106
- edge_trace = go.Scatter(
107
- x=edge_x, y=edge_y,
108
- line=dict(width=2, color='#888'),
109
- hoverinfo='none',
110
- mode='lines'
111
- )
112
-
113
- node_trace = go.Scatter(
114
- x=node_x, y=node_y,
115
- mode='markers+text',
116
- text=node_text,
117
- textposition="middle center",
118
- marker=dict(
119
- symbol='square',
120
- size=[50] * len(node_x), # Reduced size for better fit
121
- color='#4CAF50',
122
- line=dict(width=2, color='DarkSlateGrey')
123
- ),
124
- hoverinfo='text',
125
- textfont=dict(size=12, color='white')
126
- )
127
-
128
- fig = go.Figure(data=[edge_trace, node_trace],
129
- layout=go.Layout(
130
- title=f'<b>{title}</b><br>',
131
- titlefont_size=20,
132
- showlegend=False,
133
- hovermode='closest',
134
- margin=dict(b=20, l=5, r=5, t=40),
135
- xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
136
- yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
137
- )
138
 
139
- return fig
140
 
141
  # Function to download CSV template
142
  def download_csv_template():
@@ -194,8 +131,10 @@ CFO,Accounting Manager"""
194
  # Generate chart
195
  if st.button("Generate Organization Chart"):
196
  if not edited_df.empty:
197
- fig = create_org_chart(edited_df, chart_title)
198
- st.plotly_chart(fig, use_container_width=True)
 
 
199
 
200
  # PDF download options
201
  st.markdown("### Download Options")
@@ -207,12 +146,10 @@ CFO,Accounting Manager"""
207
 
208
  # Convert to PDF
209
  if st.button("Download as PDF"):
210
- img_bytes = fig.to_image(format="pdf",
211
- width=800 if orientation == "Portrait" else 1200,
212
- height=1200 if orientation == "Portrait" else 800)
213
  st.download_button(
214
  label="Download PDF",
215
- data=img_bytes,
216
  file_name="org_chart.pdf",
217
  mime="application/pdf"
218
  )
 
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("""
 
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
  # 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
 
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
  )