Spaces:
Sleeping
Sleeping
Initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import openpyxl
|
3 |
+
from openpyxl.styles import PatternFill
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
def load_excel(file_path):
|
7 |
+
"""Loads the Excel file into a pandas DataFrame."""
|
8 |
+
df = pd.read_excel(file_path, sheet_name="Sheet1")
|
9 |
+
return df
|
10 |
+
|
11 |
+
def highlight_pairs(df, threshold=2.0):
|
12 |
+
"""Highlights pairs in columns D and E based on the threshold."""
|
13 |
+
colors = ["FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF"] # Hex colors for RGB
|
14 |
+
color_index = 0
|
15 |
+
|
16 |
+
df['High Result'] = ''
|
17 |
+
df['Low Result'] = ''
|
18 |
+
|
19 |
+
colored_pairs = set()
|
20 |
+
wb = openpyxl.load_workbook(file_path)
|
21 |
+
ws = wb['Sheet1']
|
22 |
+
|
23 |
+
# Function to apply color to cells
|
24 |
+
def apply_color(cell, color_hex):
|
25 |
+
cell.fill = PatternFill(start_color=color_hex, end_color=color_hex, fill_type="solid")
|
26 |
+
|
27 |
+
last_row = len(df)
|
28 |
+
|
29 |
+
def process_column(df, col_index, output_col, color_pairs, colors):
|
30 |
+
nonlocal color_index
|
31 |
+
for i in range(last_row - 1, 1, -1):
|
32 |
+
for j in range(i - 1, 1, -1):
|
33 |
+
if abs(df.iloc[i, col_index] - df.iloc[j, col_index]) <= threshold:
|
34 |
+
pair_key_i = (i, col_index)
|
35 |
+
pair_key_j = (j, col_index)
|
36 |
+
|
37 |
+
if pair_key_i not in color_pairs and pair_key_j not in color_pairs:
|
38 |
+
apply_color(ws.cell(row=i+2, column=col_index+1), colors[color_index])
|
39 |
+
apply_color(ws.cell(row=j+2, column=col_index+1), colors[color_index])
|
40 |
+
|
41 |
+
color_pairs.add(pair_key_i)
|
42 |
+
color_pairs.add(pair_key_j)
|
43 |
+
|
44 |
+
output_value = (max if col_index == 3 else min)(
|
45 |
+
int(df.iloc[i, col_index]), int(df.iloc[j, col_index])
|
46 |
+
) + (1 if col_index == 3 else -1)
|
47 |
+
|
48 |
+
color_output = False
|
49 |
+
for k in range(i - 1, 1, -1):
|
50 |
+
if ((df.iloc[k, col_index] > output_value and col_index == 3) or
|
51 |
+
(df.iloc[k, col_index] < output_value and col_index == 4)):
|
52 |
+
if ((df.iloc[k, 5] > max(df.iloc[i, col_index], df.iloc[j, col_index]) and col_index == 3) or
|
53 |
+
(df.iloc[k, 5] < min(df.iloc[i, col_index], df.iloc[j, col_index]) and col_index == 4)):
|
54 |
+
apply_color(ws.cell(row=j+2, column=output_col+1), colors[color_index])
|
55 |
+
color_output = True
|
56 |
+
break
|
57 |
+
|
58 |
+
ws.cell(row=j+2, column=output_col+1).value = output_value
|
59 |
+
if not color_output:
|
60 |
+
ws.cell(row=j+2, column=output_col+1).fill = PatternFill(fill_type=None)
|
61 |
+
|
62 |
+
color_index = (color_index + 1) % len(colors)
|
63 |
+
|
64 |
+
process_column(df, 3, 6, colored_pairs, colors) # Process Column D (High Price)
|
65 |
+
process_column(df, 4, 7, colored_pairs, colors) # Process Column E (Low Price)
|
66 |
+
|
67 |
+
wb.save('output.xlsx')
|
68 |
+
|
69 |
+
def gradio_app(file_path):
|
70 |
+
"""Main function to handle Gradio inputs and outputs."""
|
71 |
+
df = load_excel(file_path)
|
72 |
+
highlight_pairs(df)
|
73 |
+
return 'output.xlsx'
|
74 |
+
|
75 |
+
# Gradio Interface
|
76 |
+
gr_interface = gr.Interface(
|
77 |
+
fn=gradio_app,
|
78 |
+
inputs=gr.inputs.File(label="Upload Excel File"),
|
79 |
+
outputs=gr.outputs.File(label="Download Processed Excel File"),
|
80 |
+
title="Excel Highlighter"
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
gr_interface.launch()
|