arif670 commited on
Commit
a34c24c
Β·
verified Β·
1 Parent(s): 7d4edbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -46
app.py CHANGED
@@ -5,54 +5,73 @@ import os
5
  import base64
6
  from io import StringIO
7
 
8
- # Add Graphviz to system PATH (critical for Hugging Face Spaces)
9
  os.environ["PATH"] += os.pathsep + '/usr/bin/graphviz'
10
 
11
- # Custom CSS for professional UI
12
  st.markdown("""
13
  <style>
14
- .stDownloadButton button {
15
- background: linear-gradient(45deg, #4CAF50, #45a049) !important;
16
- color: white !important;
17
- border-radius: 25px !important;
18
- padding: 12px 28px !important;
19
- transition: all 0.3s !important;
20
  }
21
- .stDownloadButton button:hover {
 
 
 
 
 
 
 
 
 
 
 
 
22
  transform: scale(1.05);
23
- box-shadow: 0 4px 8px rgba(0,0,0,0.2);
 
 
 
 
 
 
 
24
  }
25
  </style>
26
  """, unsafe_allow_html=True)
27
 
28
  def create_org_chart(df, title):
29
- """Create professional organization chart with Graphviz"""
30
  dot = Digraph(comment=title)
31
  dot.attr(rankdir='TB', labelloc='t', label=title,
32
- fontsize='24', fontname='Arial', margin='0.5')
33
- dot.attr('node', shape='box', style='filled',
34
- fillcolor='linear-gradient(#4CAF50, #45a049)', fontname='Arial',
35
- fontcolor='white', margin='0.3', width='2', height='0.8',
36
- fixedsize='false', color='#2c3e50', fontsize='14')
37
- dot.attr('edge', color='#666666', arrowsize='0.8')
 
 
 
 
 
 
 
38
 
39
  # Process relationships
40
- relationships = set()
41
  for _, row in df.iterrows():
42
  parent = row['Parent'].strip()
43
  child = row['Child'].strip()
44
  if parent and child:
45
- relationships.add((parent, child))
46
-
47
- # Add nodes and edges
48
- nodes = set()
49
- for parent, child in relationships:
50
- nodes.add(parent)
51
- nodes.add(child)
52
- dot.edge(parent, child)
53
-
54
- for node in nodes:
55
- dot.node(node, node)
56
 
57
  return dot
58
 
@@ -60,42 +79,57 @@ def main():
60
  st.title("🏒 Professional Organization Chart Generator")
61
 
62
  with st.sidebar:
63
- st.header("Configuration")
64
  chart_title = st.text_input("Chart Title", "Company Structure")
65
- st.markdown("### CSV Instructions")
66
- st.write("1. Download template below\n2. Edit in Excel/Sheets\n3. Upload your version")
 
67
 
68
  # CSV template download
69
  template = "Parent,Child\nCEO,CTO\nCEO,CFO\nCTO,Engineering Manager"
70
  b64 = base64.b64encode(template.encode()).decode()
71
- href = f'<a href="data:file/csv;base64,{b64}" download="template.csv">πŸ“₯ Download Template</a>'
72
  st.markdown(href, unsafe_allow_html=True)
73
 
74
- uploaded_file = st.file_uploader("Upload CSV File", type=["csv"])
 
75
 
76
  if uploaded_file:
77
  try:
78
  df = pd.read_csv(uploaded_file)
79
  if {'Parent', 'Child'}.issubset(df.columns):
80
- with st.spinner("Generating professional chart..."):
81
  chart = create_org_chart(df, chart_title)
82
 
83
  # Display chart
84
- st.graphviz_chart(chart, use_container_width=True)
 
 
85
 
86
  # PDF Generation
87
- pdf_bytes = chart.pipe(format='pdf')
88
- st.download_button(
89
- label="πŸ“₯ Download PDF Report",
90
- data=pdf_bytes,
91
- file_name=f"{chart_title.replace(' ', '_')}.pdf",
92
- mime="application/pdf",
93
- help="Download high-quality PDF version"
94
- )
 
 
 
 
 
 
 
 
 
 
 
95
  else:
96
- st.error("CSV must contain 'Parent' and 'Child' columns")
97
  except Exception as e:
98
- st.error(f"Error processing file: {str(e)}")
99
 
100
  if __name__ == "__main__":
101
  main()
 
5
  import base64
6
  from io import StringIO
7
 
8
+ # Fix Graphviz PATH error for Hugging Face
9
  os.environ["PATH"] += os.pathsep + '/usr/bin/graphviz'
10
 
11
+ # Enhanced UI CSS
12
  st.markdown("""
13
  <style>
14
+ .stApp {
15
+ background: #f8f9fa;
16
+ font-family: 'Segoe UI', system-ui;
 
 
 
17
  }
18
+ .st-emotion-cache-1kyxreq {
19
+ justify-content: center;
20
+ }
21
+ .stButton>button {
22
+ background: linear-gradient(135deg, #4CAF50, #45a049);
23
+ color: white;
24
+ border-radius: 8px;
25
+ padding: 12px 24px;
26
+ border: none;
27
+ font-weight: 600;
28
+ transition: transform 0.3s;
29
+ }
30
+ .stButton>button:hover {
31
  transform: scale(1.05);
32
+ box-shadow: 0 4px 12px rgba(76,175,80,0.3);
33
+ }
34
+ .stDownloadButton>button {
35
+ background: linear-gradient(135deg, #2196F3, #1976D2);
36
+ }
37
+ .st-eb {
38
+ border-radius: 10px;
39
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
40
  }
41
  </style>
42
  """, unsafe_allow_html=True)
43
 
44
  def create_org_chart(df, title):
45
+ """Create professional org chart with straight lines"""
46
  dot = Digraph(comment=title)
47
  dot.attr(rankdir='TB', labelloc='t', label=title,
48
+ fontsize='24', fontname='Arial', margin='0.5',
49
+ splines='ortho') # Force straight lines
50
+
51
+ # Node styling
52
+ dot.attr('node', shape='box', style='filled',
53
+ fillcolor='linear-gradient(#4CAF50, #45a049)',
54
+ fontname='Arial', fontcolor='white',
55
+ margin='0.4', width='2', height='0.9',
56
+ fixedsize='false', fontsize='14')
57
+
58
+ # Edge styling for straight lines
59
+ dot.attr('edge', color='#666666', arrowsize='0.8',
60
+ penwidth='1.2', dir='forward')
61
 
62
  # Process relationships
63
+ added_nodes = set()
64
  for _, row in df.iterrows():
65
  parent = row['Parent'].strip()
66
  child = row['Child'].strip()
67
  if parent and child:
68
+ if parent not in added_nodes:
69
+ dot.node(parent)
70
+ added_nodes.add(parent)
71
+ if child not in added_nodes:
72
+ dot.node(child)
73
+ added_nodes.add(child)
74
+ dot.edge(parent, child)
 
 
 
 
75
 
76
  return dot
77
 
 
79
  st.title("🏒 Professional Organization Chart Generator")
80
 
81
  with st.sidebar:
82
+ st.header("βš™οΈ Configuration")
83
  chart_title = st.text_input("Chart Title", "Company Structure")
84
+ st.markdown("---")
85
+ st.markdown("### πŸ“‹ CSV Instructions")
86
+ st.write("1. Download template\n2. Edit in Excel/Sheets\n3. Upload your file")
87
 
88
  # CSV template download
89
  template = "Parent,Child\nCEO,CTO\nCEO,CFO\nCTO,Engineering Manager"
90
  b64 = base64.b64encode(template.encode()).decode()
91
+ href = f'<a href="data:file/csv;base64,{b64}" download="template.csv" style="text-decoration: none; color: white; background: #4CAF50; padding: 10px 20px; border-radius: 8px; display: inline-block; margin-top: 10px;">πŸ“₯ Download Template</a>'
92
  st.markdown(href, unsafe_allow_html=True)
93
 
94
+ uploaded_file = st.file_uploader("πŸ“€ Upload CSV File", type=["csv"],
95
+ help="Upload your organizational hierarchy CSV file")
96
 
97
  if uploaded_file:
98
  try:
99
  df = pd.read_csv(uploaded_file)
100
  if {'Parent', 'Child'}.issubset(df.columns):
101
+ with st.spinner("πŸ” Analyzing structure..."):
102
  chart = create_org_chart(df, chart_title)
103
 
104
  # Display chart
105
+ col1, col2 = st.columns([3, 1])
106
+ with col1:
107
+ st.graphviz_chart(chart, use_container_width=True)
108
 
109
  # PDF Generation
110
+ with col2:
111
+ st.markdown("### πŸ“€ Export Options")
112
+ pdf_bytes = chart.pipe(format='pdf')
113
+ st.download_button(
114
+ label="πŸ“„ Download PDF",
115
+ data=pdf_bytes,
116
+ file_name=f"{chart_title.replace(' ', '_')}.pdf",
117
+ mime="application/pdf",
118
+ help="Download high-quality PDF version"
119
+ )
120
+
121
+ png_bytes = chart.pipe(format='png')
122
+ st.download_button(
123
+ label="πŸ–ΌοΈ Download PNG",
124
+ data=png_bytes,
125
+ file_name=f"{chart_title.replace(' ', '_')}.png",
126
+ mime="image/png",
127
+ help="Download image version"
128
+ )
129
  else:
130
+ st.error("❌ CSV must contain 'Parent' and 'Child' columns")
131
  except Exception as e:
132
+ st.error(f"🚨 Error processing file: {str(e)}")
133
 
134
  if __name__ == "__main__":
135
  main()