cs70 commited on
Commit
c74b3e5
·
verified ·
1 Parent(s): 211f41a

Use by default name of the sheet and remove colors if colorized already

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -1,14 +1,23 @@
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
@@ -17,8 +26,9 @@ def highlight_pairs(df, threshold=2.0):
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):
@@ -68,8 +78,8 @@ def highlight_pairs(df, threshold=2.0):
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
 
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
 
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):
 
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