Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,26 @@
|
|
| 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("""
|
| 9 |
<style>
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
background-color: #f9f9f9;
|
| 13 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
/* Titles */
|
| 17 |
-
h1 {
|
| 18 |
-
color: #2c3e50;
|
| 19 |
-
border-bottom: 3px solid #4CAF50;
|
| 20 |
-
padding-bottom: 10px;
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
/* Sidebar */
|
| 24 |
-
.stSidebar {
|
| 25 |
-
background-color: #2c3e50;
|
| 26 |
color: white !important;
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
-
|
| 29 |
-
/* Buttons */
|
| 30 |
-
.stButton>button {
|
| 31 |
-
background-color: #4CAF50;
|
| 32 |
-
color: white;
|
| 33 |
-
border-radius: 25px;
|
| 34 |
-
padding: 12px 28px;
|
| 35 |
-
border: none;
|
| 36 |
-
font-size: 16px;
|
| 37 |
-
transition: all 0.3s;
|
| 38 |
-
}
|
| 39 |
-
.stButton>button:hover {
|
| 40 |
-
background-color: #45a049;
|
| 41 |
transform: scale(1.05);
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
/* File uploader */
|
| 45 |
-
.stFileUploader>div>div>div>div {
|
| 46 |
-
background-color: #ffffff;
|
| 47 |
-
border-radius: 10px;
|
| 48 |
-
padding: 15px;
|
| 49 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
/* Data editor */
|
| 53 |
-
.stDataEditor {
|
| 54 |
-
background-color: white;
|
| 55 |
-
border-radius: 10px;
|
| 56 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
/* Tabs */
|
| 60 |
-
.stTabs [data-baseweb="tab-list"] {
|
| 61 |
-
gap: 10px;
|
| 62 |
-
}
|
| 63 |
-
.stTabs [data-baseweb="tab"] {
|
| 64 |
-
background-color: #e8f5e9;
|
| 65 |
-
border-radius: 8px;
|
| 66 |
-
padding: 12px 24px;
|
| 67 |
-
transition: all 0.3s;
|
| 68 |
-
}
|
| 69 |
-
.stTabs [aria-selected="true"] {
|
| 70 |
-
background-color: #4CAF50;
|
| 71 |
-
color: white !important;
|
| 72 |
}
|
| 73 |
</style>
|
| 74 |
""", unsafe_allow_html=True)
|
|
@@ -76,97 +28,74 @@ st.markdown("""
|
|
| 76 |
def create_org_chart(df, title):
|
| 77 |
"""Create professional organization chart with Graphviz"""
|
| 78 |
dot = Digraph(comment=title)
|
| 79 |
-
dot.attr(rankdir='TB', labelloc='t', label=title,
|
|
|
|
| 80 |
dot.attr('node', shape='box', style='filled',
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
dot.attr('edge', color='#666666', arrowsize='0.8')
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
edges = set()
|
| 89 |
-
|
| 90 |
for _, row in df.iterrows():
|
| 91 |
parent = row['Parent'].strip()
|
| 92 |
child = row['Child'].strip()
|
| 93 |
-
|
| 94 |
if parent and child:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
for node in nodes:
|
| 101 |
dot.node(node, node)
|
| 102 |
-
|
| 103 |
-
# Add edges
|
| 104 |
-
for edge in edges:
|
| 105 |
-
dot.edge(edge[0], edge[1])
|
| 106 |
-
|
| 107 |
return dot
|
| 108 |
|
| 109 |
-
def download_csv_template():
|
| 110 |
-
"""Generate CSV template download link"""
|
| 111 |
-
csv_template = "Parent,Child\nCEO,CTO\nCEO,CFO\nCTO,Engineering Manager\nCFO,Accounting Manager"
|
| 112 |
-
b64 = base64.b64encode(csv_template.encode()).decode()
|
| 113 |
-
href = f'<a href="data:file/csv;base64,{b64}" download="org_chart_template.csv" style="\
|
| 114 |
-
background-color: #4CAF50; color: white; padding: 12px 24px; \
|
| 115 |
-
border-radius: 25px; text-decoration: none;">Download CSV Template</a>'
|
| 116 |
-
st.markdown(href, unsafe_allow_html=True)
|
| 117 |
-
|
| 118 |
def main():
|
| 119 |
st.title("🏢 Professional Organization Chart Generator")
|
| 120 |
|
| 121 |
with st.sidebar:
|
| 122 |
st.header("Configuration")
|
| 123 |
-
chart_title = st.text_input("Chart Title", "Company
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
tab1, tab2 = st.tabs(["📥 Data Input", "📊 Organization Chart"])
|
| 128 |
-
|
| 129 |
-
with tab1:
|
| 130 |
-
st.subheader("1. Prepare Your Data")
|
| 131 |
-
download_csv_template()
|
| 132 |
-
|
| 133 |
-
uploaded_file = st.file_uploader("Upload CSV File", type=["csv"],
|
| 134 |
-
help="Upload your organizational data in CSV format")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
CEO,CFO
|
| 142 |
-
CTO,Engineering Manager
|
| 143 |
-
CFO,Accounting Manager"""
|
| 144 |
-
df = pd.read_csv(StringIO(default_data))
|
| 145 |
-
|
| 146 |
-
edited_df = st.data_editor(df, num_rows="dynamic", use_container_width=True)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
with st.spinner("Generating professional chart..."):
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
except Exception as e:
|
| 167 |
-
st.error(f"Error generating chart: {str(e)}")
|
| 168 |
else:
|
| 169 |
-
st.
|
|
|
|
|
|
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from graphviz import Digraph
|
| 4 |
+
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)
|
|
|
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
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()
|