Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import io
|
|
6 |
import base64
|
7 |
from matplotlib.patches import Rectangle
|
8 |
import matplotlib.gridspec as gridspec
|
|
|
9 |
|
10 |
# 定义全局常量
|
11 |
SPLIT_TIME = "17:30" # 分界线时间
|
@@ -76,62 +77,82 @@ def create_print_layout(data, title):
|
|
76 |
if data.empty:
|
77 |
return None
|
78 |
|
79 |
-
# 设置 A5
|
80 |
fig = plt.figure(figsize=(5.83, 8.27), dpi=300)
|
81 |
|
82 |
# 减小边距,最大化利用空间
|
83 |
-
plt.subplots_adjust(left=0.
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# 创建网格,减小间距
|
86 |
-
|
87 |
-
gs = gridspec.GridSpec(rows, 3, hspace=0.1, wspace=0.1) # 显著减小间距
|
88 |
|
89 |
# 设置字体和样式
|
90 |
plt.rcParams['font.family'] = 'sans-serif'
|
91 |
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体
|
92 |
|
93 |
-
#
|
94 |
-
base_fontsize = min(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
# 填充数据
|
97 |
-
for idx, (hall, end_time) in enumerate(
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
spine.
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
124 |
|
125 |
# 将图表转换为 base64 字符串
|
126 |
buffer = io.BytesIO()
|
127 |
-
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.05)
|
128 |
buffer.seek(0)
|
129 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
130 |
plt.close()
|
131 |
|
132 |
return f'data:image/png;base64,{image_base64}'
|
133 |
|
134 |
-
|
135 |
# Streamlit 界面
|
136 |
st.set_page_config(page_title="散厅时间快捷打印", layout="wide")
|
137 |
st.title("散厅时间快捷打印")
|
@@ -161,4 +182,4 @@ if uploaded_file:
|
|
161 |
if part2_image:
|
162 |
st.image(part2_image)
|
163 |
else:
|
164 |
-
st.info("夜班部分没有数据")
|
|
|
6 |
import base64
|
7 |
from matplotlib.patches import Rectangle
|
8 |
import matplotlib.gridspec as gridspec
|
9 |
+
import math
|
10 |
|
11 |
# 定义全局常量
|
12 |
SPLIT_TIME = "17:30" # 分界线时间
|
|
|
77 |
if data.empty:
|
78 |
return None
|
79 |
|
80 |
+
# 设置 A5 纸张竖向尺寸(单位:英寸)
|
81 |
fig = plt.figure(figsize=(5.83, 8.27), dpi=300)
|
82 |
|
83 |
# 减小边距,最大化利用空间
|
84 |
+
plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05)
|
85 |
+
|
86 |
+
# 计算行数和列数
|
87 |
+
total_items = len(data)
|
88 |
+
num_cols = 3 # 固定为3列
|
89 |
+
num_rows = math.ceil(total_items / num_cols) # 计算需要的行数
|
90 |
|
91 |
# 创建网格,减小间距
|
92 |
+
gs = gridspec.GridSpec(num_rows, num_cols, hspace=0.1, wspace=0.1)
|
|
|
93 |
|
94 |
# 设置字体和样式
|
95 |
plt.rcParams['font.family'] = 'sans-serif'
|
96 |
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体
|
97 |
|
98 |
+
# 计算适合的字体大小(考虑到竖向布局和3列)
|
99 |
+
base_fontsize = min(50, 280 / num_rows) # 调整基础字体大小
|
100 |
+
|
101 |
+
# 重新组织数据为竖向排列
|
102 |
+
reshaped_data = []
|
103 |
+
data_values = data.values.tolist()
|
104 |
+
|
105 |
+
# 填充空值确保数据长度是3的倍数
|
106 |
+
while len(data_values) % 3 != 0:
|
107 |
+
data_values.append(['', ''])
|
108 |
+
|
109 |
+
# 按竖向顺序重新排列数据
|
110 |
+
num_rows = len(data_values) // 3
|
111 |
+
for col in range(3):
|
112 |
+
for row in range(num_rows):
|
113 |
+
idx = row + col * num_rows
|
114 |
+
if idx < len(data_values):
|
115 |
+
reshaped_data.append(data_values[idx])
|
116 |
|
117 |
# 填充数据
|
118 |
+
for idx, (hall, end_time) in enumerate(reshaped_data):
|
119 |
+
if hall and end_time: # 只处理非空数据
|
120 |
+
row = idx // 3
|
121 |
+
col = idx % 3
|
122 |
+
|
123 |
+
# 创建子图
|
124 |
+
ax = plt.subplot(gs[row, col])
|
125 |
+
|
126 |
+
# 设置更深的边框颜色
|
127 |
+
for spine in ax.spines.values():
|
128 |
+
spine.set_color(BORDER_COLOR)
|
129 |
+
spine.set_linewidth(0.5)
|
130 |
+
|
131 |
+
# 将影厅和时间紧密连接显示
|
132 |
+
display_text = f"{hall}{end_time}"
|
133 |
+
ax.text(0.5, 0.5, display_text,
|
134 |
+
fontsize=base_fontsize,
|
135 |
+
fontweight='bold',
|
136 |
+
ha='center',
|
137 |
+
va='center')
|
138 |
+
|
139 |
+
# 设置更小的内边距
|
140 |
+
ax.set_xlim(-0.02, 1.02)
|
141 |
+
ax.set_ylim(-0.02, 1.02)
|
142 |
+
|
143 |
+
# 移除坐标轴
|
144 |
+
ax.set_xticks([])
|
145 |
+
ax.set_yticks([])
|
146 |
|
147 |
# 将图表转换为 base64 字符串
|
148 |
buffer = io.BytesIO()
|
149 |
+
plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.05)
|
150 |
buffer.seek(0)
|
151 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
152 |
plt.close()
|
153 |
|
154 |
return f'data:image/png;base64,{image_base64}'
|
155 |
|
|
|
156 |
# Streamlit 界面
|
157 |
st.set_page_config(page_title="散厅时间快捷打印", layout="wide")
|
158 |
st.title("散厅时间快捷打印")
|
|
|
182 |
if part2_image:
|
183 |
st.image(part2_image)
|
184 |
else:
|
185 |
+
st.info("夜班部分没有数据")
|