Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,7 @@ import pandas as pd
|
|
2 |
import openpyxl
|
3 |
import gradio as gr
|
4 |
import io
|
5 |
-
import
|
6 |
-
from datetime import datetime
|
7 |
|
8 |
def convert_schedule(file_path, direction):
|
9 |
try:
|
@@ -100,39 +99,76 @@ def convert_schedule(file_path, direction):
|
|
100 |
# For display, include index as a column
|
101 |
display_df = result.reset_index().rename(columns={'index': first_col_name})
|
102 |
|
103 |
-
# 6.
|
104 |
result_clean = result.copy().fillna('OFF')
|
105 |
|
106 |
# Ensure all values are strings
|
107 |
for col in result_clean.columns:
|
108 |
result_clean[col] = result_clean[col].astype(str)
|
109 |
|
110 |
-
#
|
111 |
-
|
|
|
|
|
112 |
|
113 |
except Exception as e:
|
114 |
error_df = pd.DataFrame({'Error': [f"Error processing file: {str(e)}"]})
|
115 |
-
return error_df,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
#
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|
|
|
2 |
import openpyxl
|
3 |
import gradio as gr
|
4 |
import io
|
5 |
+
import os
|
|
|
6 |
|
7 |
def convert_schedule(file_path, direction):
|
8 |
try:
|
|
|
99 |
# For display, include index as a column
|
100 |
display_df = result.reset_index().rename(columns={'index': first_col_name})
|
101 |
|
102 |
+
# 6. Create CSV content as string for download
|
103 |
result_clean = result.copy().fillna('OFF')
|
104 |
|
105 |
# Ensure all values are strings
|
106 |
for col in result_clean.columns:
|
107 |
result_clean[col] = result_clean[col].astype(str)
|
108 |
|
109 |
+
# Convert to CSV string
|
110 |
+
csv_content = result_clean.to_csv(index=True)
|
111 |
+
|
112 |
+
return display_df, csv_content
|
113 |
|
114 |
except Exception as e:
|
115 |
error_df = pd.DataFrame({'Error': [f"Error processing file: {str(e)}"]})
|
116 |
+
return error_df, "Error occurred during conversion"
|
117 |
+
|
118 |
+
def download_csv(csv_content):
|
119 |
+
"""Helper function to create downloadable CSV"""
|
120 |
+
if csv_content and csv_content != "Error occurred during conversion":
|
121 |
+
# Create a temporary file
|
122 |
+
filename = "converted_schedule.csv"
|
123 |
+
with open(filename, 'w', newline='', encoding='utf-8') as f:
|
124 |
+
f.write(csv_content)
|
125 |
+
return filename
|
126 |
+
return None
|
127 |
|
128 |
+
# Create the interface with blocks for better control
|
129 |
+
with gr.Blocks(title="7-Day Schedule Converter") as iface:
|
130 |
+
gr.Markdown("# 7-Day Schedule Converter")
|
131 |
+
gr.Markdown("Upload a 7-column weekly schedule (Models vs Days) with merged or single headers, then flip between Models→Texters or Texters→Models.")
|
132 |
+
|
133 |
+
with gr.Row():
|
134 |
+
with gr.Column():
|
135 |
+
file_input = gr.File(label='Upload Weekly Schedule (.xlsx)', file_count='single', type='filepath')
|
136 |
+
direction_input = gr.Radio(['A to B', 'B to A'], label='Convert Direction', value='A to B')
|
137 |
+
convert_btn = gr.Button("Convert Schedule", variant="primary")
|
138 |
+
|
139 |
+
with gr.Column():
|
140 |
+
output_df = gr.Dataframe(label='Converted Schedule')
|
141 |
+
download_btn = gr.Button("Download as CSV", variant="secondary")
|
142 |
+
download_file = gr.File(label="Download", visible=False)
|
143 |
+
|
144 |
+
# Store CSV content in state
|
145 |
+
csv_state = gr.State()
|
146 |
+
|
147 |
+
# Convert button click
|
148 |
+
def convert_and_store(file_path, direction):
|
149 |
+
display_df, csv_content = convert_schedule(file_path, direction)
|
150 |
+
return display_df, csv_content
|
151 |
+
|
152 |
+
convert_btn.click(
|
153 |
+
fn=convert_and_store,
|
154 |
+
inputs=[file_input, direction_input],
|
155 |
+
outputs=[output_df, csv_state]
|
156 |
+
)
|
157 |
+
|
158 |
+
# Download button click
|
159 |
+
def create_download(csv_content):
|
160 |
+
if csv_content and csv_content != "Error occurred during conversion":
|
161 |
+
filename = "converted_schedule.csv"
|
162 |
+
with open(filename, 'w', newline='', encoding='utf-8') as f:
|
163 |
+
f.write(csv_content)
|
164 |
+
return gr.File(value=filename, visible=True)
|
165 |
+
return gr.File(visible=False)
|
166 |
+
|
167 |
+
download_btn.click(
|
168 |
+
fn=create_download,
|
169 |
+
inputs=[csv_state],
|
170 |
+
outputs=[download_file]
|
171 |
+
)
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|