oh-my-dear-ai commited on
Commit
f83d96d
1 Parent(s): 24aa3b8

refactor(app): update font setting and encoding functions

Browse files

- Remove the font setting code for "Arial Unicode MS" in matplotlib as it may no longer be
necessary or has been changed.
- Comment out the base64 encoding function that was previously used for token generation,
indicating it's no longer in use but not deleting it in case it's needed later.
- Simplify the binary list to base64 string conversion logic.
- Adjust the function comments to reflect the changes made in the encoding functions.
- Reorder the plot library preference in `process_data` function parameters.
- Update the text wrapping and cell text property setting in `generate_img_by_matplotlib`
to improve code readability and maintainability.
- Swap the default selection of the plot library radio button to prefer "plotly" over
"matplotlib".

This change ensures consistency and potentially improves performance by removing or
simplifying unused or inefficient code sections. It also prioritizes the use of "plotly"
over "matplotlib" as the preferred plotting library.

Files changed (1) hide show
  1. app.py +13 -29
app.py CHANGED
@@ -25,23 +25,6 @@ columns_with_tasks = [
25
  "color6",
26
  ]
27
  PLANTS = list(df["plant"].unique())
28
- # 更改字体设置
29
- # rc("font", family="Arial Unicode MS")
30
-
31
- # def decimal2base64(decimal_int):
32
- # # 定义 64 进制字符集
33
- # base64_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
34
-
35
- # if decimal_int == 0:
36
- # return "0"
37
-
38
- # base64_str = ""
39
- # while decimal_int > 0:
40
- # remainder = decimal_int % 64
41
- # base64_str = base64_chars[remainder] + base64_str
42
- # decimal_int //= 64
43
-
44
- # return base64_str
45
 
46
 
47
  def bin_ls2base64(ls):
@@ -55,8 +38,8 @@ def bin_ls2base64(ls):
55
 
56
 
57
  def base64_to_binary(base64_str):
 
58
  if isinstance(base64_str, str):
59
- # 将base64字符串转换为二进制列表
60
  byte_str = base64.b64decode(base64_str)
61
  binary_str = bin(int.from_bytes(byte_str, byteorder="big"))[2:].zfill(
62
  len(columns_with_tasks) * len(PLANTS)
@@ -85,7 +68,7 @@ def parse_token(token):
85
  print(e)
86
 
87
 
88
- # 定义一个简单的函数,模拟接收DataFrame数据
89
  def process_data(*args):
90
  plot_library = args[-1]
91
  almanac_dict = dict(zip(PLANTS, args[:-1]))
@@ -107,7 +90,7 @@ def process_data(*args):
107
  almanac_df.loc[almanac_df["plant"] == pl, columns_with_tasks[i]] = "✔"
108
 
109
  almanac_reverse_64 = bin_ls2base64(reversed(almanac_bin_ls))
110
- # 这里可以添加处理DataFrame的逻辑
111
  return (
112
  (
113
  generate_img_by_plotly(almanac_df.drop(columns=df.columns[0]))
@@ -143,6 +126,9 @@ def show_checkbox_groups(token):
143
  return checklist_inputs
144
 
145
 
 
 
 
146
  def wrap_text(text, max_width=20):
147
  """Manually wrap text based on max_width (character count)"""
148
  wrapped_lines = []
@@ -184,8 +170,9 @@ def generate_img_by_matplotlib(df):
184
  if cell.get_text().get_text() == "nan":
185
  cell.set_text_props(text="", ha="center")
186
  if row == 0:
 
187
  cell.set_text_props(
188
- weight="bold", ha="center"
189
  ) # Bold and center-align header text
190
  if col == 0:
191
  text = cell.get_text().get_text()
@@ -220,6 +207,9 @@ def generate_img_by_matplotlib(df):
220
  return image
221
 
222
 
 
 
 
223
  def color_mapping(color):
224
  color_hex = {
225
  "turquoise": "#28b6aa",
@@ -373,9 +363,9 @@ with gr.Blocks() as app:
373
  """
374
  )
375
  plot_library = gr.Radio(
376
- ["matplotlib", "plotly"],
377
  label="Plot Library",
378
- value="matplotlib",
379
  info="Choose your plot library",
380
  )
381
 
@@ -407,12 +397,6 @@ with gr.Blocks() as app:
407
  outputs=checklist_inputs,
408
  )
409
 
410
- # generate_button.click(
411
- # generate_img,
412
- # inputs=[df_out],
413
- # outputs=[generated_img],
414
- # )
415
-
416
 
417
  app.queue()
418
  app.launch()
 
25
  "color6",
26
  ]
27
  PLANTS = list(df["plant"].unique())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  def bin_ls2base64(ls):
 
38
 
39
 
40
  def base64_to_binary(base64_str):
41
+ # 将base64字符串转换为二进制字符串
42
  if isinstance(base64_str, str):
 
43
  byte_str = base64.b64decode(base64_str)
44
  binary_str = bin(int.from_bytes(byte_str, byteorder="big"))[2:].zfill(
45
  len(columns_with_tasks) * len(PLANTS)
 
68
  print(e)
69
 
70
 
71
+ # 定义一个简单的函数,接收DataFrame数据
72
  def process_data(*args):
73
  plot_library = args[-1]
74
  almanac_dict = dict(zip(PLANTS, args[:-1]))
 
90
  almanac_df.loc[almanac_df["plant"] == pl, columns_with_tasks[i]] = "✔"
91
 
92
  almanac_reverse_64 = bin_ls2base64(reversed(almanac_bin_ls))
93
+
94
  return (
95
  (
96
  generate_img_by_plotly(almanac_df.drop(columns=df.columns[0]))
 
126
  return checklist_inputs
127
 
128
 
129
+ """使用matplotlib生成图片"""
130
+
131
+
132
  def wrap_text(text, max_width=20):
133
  """Manually wrap text based on max_width (character count)"""
134
  wrapped_lines = []
 
170
  if cell.get_text().get_text() == "nan":
171
  cell.set_text_props(text="", ha="center")
172
  if row == 0:
173
+ text = cell.get_text().get_text()
174
  cell.set_text_props(
175
+ text=text, weight="bold", ha="center"
176
  ) # Bold and center-align header text
177
  if col == 0:
178
  text = cell.get_text().get_text()
 
207
  return image
208
 
209
 
210
+ """使用plotly生成图片"""
211
+
212
+
213
  def color_mapping(color):
214
  color_hex = {
215
  "turquoise": "#28b6aa",
 
363
  """
364
  )
365
  plot_library = gr.Radio(
366
+ ["plotly", "matplotlib"],
367
  label="Plot Library",
368
+ value=""plotly",
369
  info="Choose your plot library",
370
  )
371
 
 
397
  outputs=checklist_inputs,
398
  )
399
 
 
 
 
 
 
 
400
 
401
  app.queue()
402
  app.launch()