Chiragkumar Savani commited on
Commit
47b49e7
·
1 Parent(s): c74b3e5

Using csv file as input

Browse files
Files changed (1) hide show
  1. app.py +139 -84
app.py CHANGED
@@ -1,94 +1,149 @@
 
1
  import pandas as pd
2
- import openpyxl
 
 
3
  from openpyxl.styles import PatternFill
 
4
 
5
- import gradio as gr
 
 
 
 
 
 
6
 
7
- def load_excel(file_path):
8
- """Loads the Excel file into a pandas DataFrame and selects the first sheet."""
9
- wb = openpyxl.load_workbook(file_path)
10
- sheet_name = wb.sheetnames[0] # Access the first sheet
11
- df = pd.read_excel(file_path, sheet_name=sheet_name)
12
- return df, wb, sheet_name
13
-
14
- def clear_sheet_colors(ws):
15
- """Removes all colors from the given worksheet."""
16
- for row in ws.iter_rows():
17
- for cell in row:
18
- cell.fill = PatternFill(fill_type=None)
19
-
20
- def highlight_pairs(df, wb, sheet_name, threshold=2.0):
21
- """Highlights pairs in columns D and E based on the threshold."""
22
- colors = ["FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF"] # Hex colors for RGB
23
- color_index = 0
24
-
25
- df['High Result'] = ''
26
- df['Low Result'] = ''
27
-
 
 
28
  colored_pairs = set()
29
- ws = wb[sheet_name]
30
-
31
- clear_sheet_colors(ws) # Clear existing colors before applying new ones
32
-
33
- # Function to apply color to cells
34
- def apply_color(cell, color_hex):
35
- cell.fill = PatternFill(start_color=color_hex, end_color=color_hex, fill_type="solid")
36
-
37
- last_row = len(df)
38
-
39
- def process_column(df, col_index, output_col, color_pairs, colors):
40
- nonlocal color_index
41
- for i in range(last_row - 1, 1, -1):
42
- for j in range(i - 1, 1, -1):
43
- if abs(df.iloc[i, col_index] - df.iloc[j, col_index]) <= threshold:
44
- pair_key_i = (i, col_index)
45
- pair_key_j = (j, col_index)
46
-
47
- if pair_key_i not in color_pairs and pair_key_j not in color_pairs:
48
- apply_color(ws.cell(row=i+2, column=col_index+1), colors[color_index])
49
- apply_color(ws.cell(row=j+2, column=col_index+1), colors[color_index])
50
-
51
- color_pairs.add(pair_key_i)
52
- color_pairs.add(pair_key_j)
53
-
54
- output_value = (max if col_index == 3 else min)(
55
- int(df.iloc[i, col_index]), int(df.iloc[j, col_index])
56
- ) + (1 if col_index == 3 else -1)
57
-
58
- color_output = False
59
- for k in range(i - 1, 1, -1):
60
- if ((df.iloc[k, col_index] > output_value and col_index == 3) or
61
- (df.iloc[k, col_index] < output_value and col_index == 4)):
62
- if ((df.iloc[k, 5] > max(df.iloc[i, col_index], df.iloc[j, col_index]) and col_index == 3) or
63
- (df.iloc[k, 5] < min(df.iloc[i, col_index], df.iloc[j, col_index]) and col_index == 4)):
64
- apply_color(ws.cell(row=j+2, column=output_col+1), colors[color_index])
65
- color_output = True
66
- break
67
-
68
- ws.cell(row=j+2, column=output_col+1).value = output_value
69
- if not color_output:
70
- ws.cell(row=j+2, column=output_col+1).fill = PatternFill(fill_type=None)
71
-
72
- color_index = (color_index + 1) % len(colors)
73
-
74
- process_column(df, 3, 6, colored_pairs, colors) # Process Column D (High Price)
75
- process_column(df, 4, 7, colored_pairs, colors) # Process Column E (Low Price)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- wb.save('output.xlsx')
78
-
79
- def gradio_app(file_path):
80
- """Main function to handle Gradio inputs and outputs."""
81
- df, wb, sheet_name = load_excel(file_path)
82
- highlight_pairs(df, wb, sheet_name)
83
- return 'output.xlsx'
84
-
85
- # Gradio Interface
86
- gr_interface = gr.Interface(
87
- fn=gradio_app,
88
- inputs=gr.File(label="Upload Excel File"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  outputs=gr.File(label="Download Processed Excel File"),
90
- title="Excel Highlighter"
 
91
  )
92
 
93
  if __name__ == "__main__":
94
- gr_interface.launch()
 
1
+ import os
2
  import pandas as pd
3
+ import numpy as np
4
+ import gradio as gr
5
+ from openpyxl import Workbook, load_workbook
6
  from openpyxl.styles import PatternFill
7
+ import random
8
 
9
+ def generate_random_light_color():
10
+ min_brightness = 0.7
11
+ while True:
12
+ r, g, b = [random.randint(128, 255) for _ in range(3)]
13
+ brightness = (r * 0.299 + g * 0.587 + b * 0.114) / 255
14
+ if brightness >= min_brightness:
15
+ return f'{r:02X}{g:02X}{b:02X}'
16
 
17
+ def calculate_threshold(value, is_high_price=True):
18
+ """
19
+ Determine the threshold based on the provided ranges.
20
+ The function checks if it's for High Price or Low Price and returns the respective threshold.
21
+ """
22
+ if 0 <= value <= 200:
23
+ return 0.20
24
+ elif 201 <= value <= 500:
25
+ return 0.70
26
+ elif 501 <= value <= 1000:
27
+ return 1.0
28
+ elif 1001 <= value <= 2000:
29
+ return 2.0
30
+ elif 2001 <= value <= 3000:
31
+ return 3.0
32
+ elif 3001 <= value <= 4000:
33
+ return 4.0
34
+ elif 4001 <= value <= 5000:
35
+ return 5.0
36
+ else:
37
+ return 5.0
38
+
39
+ def process_section(ws, start_row, end_row, col_index1, col_index2, output_col_index1, output_col_index2, high_threshold, low_threshold):
40
  colored_pairs = set()
41
+
42
+ # Process first column
43
+ for i in range(end_row, start_row - 1, -1):
44
+ for j in range(i - 1, start_row - 1, -1):
45
+ cell_value_i = ws.cell(i, col_index1).value
46
+ cell_value_j = ws.cell(j, col_index1).value
47
+ if not (isinstance(cell_value_i, (int, float)) and isinstance(cell_value_j, (int, float))):
48
+ continue
49
+ if abs(cell_value_i - cell_value_j) <= high_threshold:
50
+ if (i, col_index1) not in colored_pairs and (j, col_index1) not in colored_pairs:
51
+ color = generate_random_light_color()
52
+ fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
53
+ ws.cell(i, col_index1).fill = fill
54
+ ws.cell(j, col_index1).fill = fill
55
+ colored_pairs.add((i, col_index1))
56
+ colored_pairs.add((j, col_index1))
57
+
58
+ output_value1 = max(int(cell_value_i), int(cell_value_j)) + 1
59
+ ws.cell(j, output_col_index1).value = output_value1
60
+
61
+ for k in range(j - 1, start_row - 1, -1):
62
+ cell_value_k = ws.cell(k, col_index1).value
63
+ if isinstance(cell_value_k, (int, float)) and cell_value_k > output_value1:
64
+ if ws.cell(k, 6).value > max(cell_value_i, cell_value_j):
65
+ ws.cell(j, output_col_index1).fill = fill
66
+ break
67
+
68
+ # Process second column
69
+ for i in range(end_row, start_row - 1, -1):
70
+ for j in range(i - 1, start_row - 1, -1):
71
+ cell_value_i = ws.cell(i, col_index2).value
72
+ cell_value_j = ws.cell(j, col_index2).value
73
+ if not (isinstance(cell_value_i, (int, float)) and isinstance(cell_value_j, (int, float))):
74
+ continue
75
+ if abs(cell_value_i - cell_value_j) <= low_threshold:
76
+ if (i, col_index2) not in colored_pairs and (j, col_index2) not in colored_pairs:
77
+ color = generate_random_light_color()
78
+ fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
79
+ ws.cell(i, col_index2).fill = fill
80
+ ws.cell(j, col_index2).fill = fill
81
+ colored_pairs.add((i, col_index2))
82
+ colored_pairs.add((j, col_index2))
83
+
84
+ output_value2 = min(int(cell_value_i), int(cell_value_j)) - 1
85
+ ws.cell(j, output_col_index2).value = output_value2
86
+
87
+ for k in range(j - 1, start_row - 1, -1):
88
+ cell_value_k = ws.cell(k, col_index2).value
89
+ if isinstance(cell_value_k, (int, float)) and cell_value_k < output_value2:
90
+ if ws.cell(k, 6).value < min(cell_value_i, cell_value_j):
91
+ ws.cell(j, output_col_index2).fill = fill
92
+ break
93
+
94
+ def highlight_pairs_with_rgb(input_file):
95
+ df = pd.read_csv(input_file)
96
+ df.columns = df.columns.str.strip()
97
+
98
+ # Convert to Excel and add three empty columns
99
+ excel_filename = input_file.replace(".csv", ".xlsx")
100
+ with pd.ExcelWriter(excel_filename, engine='openpyxl') as writer:
101
+ # Reorder and create Excel file with empty columns
102
+ df_new = pd.concat([df.iloc[:, :8], pd.DataFrame(columns=['', '', '']), df.iloc[:, 8:]], axis=1)
103
+ df_new.to_excel(writer, index=False, sheet_name="Sheet1")
104
 
105
+ wb = load_workbook(excel_filename)
106
+ ws = wb.active
107
+
108
+ # Determine thresholds for High Price (Column 6) and Low Price (Column 7)
109
+ high_price_col = df["HIGH PRICE"].dropna()
110
+ low_price_col = df["LOW PRICE"].dropna()
111
+
112
+ high_threshold = calculate_threshold(high_price_col.max(), is_high_price=True)
113
+ low_threshold = calculate_threshold(low_price_col.min(), is_high_price=False)
114
+
115
+ last_row = ws.max_row
116
+ col_index1, col_index2 = 6, 7 # HIGH PRICE and LOW PRICE columns in Excel
117
+ output_col_index1, output_col_index2 = 9, 10 # Empty columns added earlier
118
+
119
+ ws.cell(1, output_col_index1).value = "High Result"
120
+ ws.cell(1, output_col_index2).value = "Low Result"
121
+
122
+ start_row = 2
123
+ while start_row <= last_row:
124
+ end_row = start_row
125
+ while end_row <= last_row and ws.cell(end_row, 1).value is not None:
126
+ end_row += 1
127
+ end_row -= 1
128
+
129
+ process_section(ws, start_row, end_row, col_index1, col_index2, output_col_index1, output_col_index2, high_threshold, low_threshold)
130
+ start_row = end_row + 2
131
+
132
+ wb.save(excel_filename)
133
+ return excel_filename
134
+
135
+ def gradio_interface(input_file):
136
+ output_file = highlight_pairs_with_rgb(input_file.name)
137
+ return output_file
138
+
139
+ # Gradio app interface
140
+ iface = gr.Interface(
141
+ fn=gradio_interface,
142
+ inputs=gr.File(label="Upload CSV File (.csv)", file_count="single"),
143
  outputs=gr.File(label="Download Processed Excel File"),
144
+ title="CSV to Excel Processor with Cell Highlighting",
145
+ description="Upload a CSV file with stock data, and download a processed Excel file with highlighted cells."
146
  )
147
 
148
  if __name__ == "__main__":
149
+ iface.launch()