Spaces:
Sleeping
Sleeping
Update app.py
Browse files8 unmodified lines9101112131415161718192021222324173 unmodified lines1981992002012022032044 unmodified lines20921021121221321421516 unmodified lines2322332342358 unmodified lines# 注册思源黑体字体font_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SourceHanSansSC-Normal-2.otf')# 添加字体fm.fontManager.addfont(font_path)# 设置字体plt.rcParams['font.family'] = ['Source Han Sans SC Normal']# 设置全局字体属性plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题# 设置页面标题st.title('商品价格区间分析工具')# 上传文件功能uploaded_file = st.file_uploader("上传商品数据Excel文件", type=['xlsx', 'csv'])173 unmodified lines# 应用自定义字体到表格文本for (i, j), cell in table.get_celld().items():text = cell.get_text()text.set_fontproperties(fm.FontProperties(fname=font_path))# 保存图像到内存buf = io.BytesIO()4 unmodified linesst.subheader('价格区间分析图表')st.image(buf, use_container_width=True)# 提供下载链接st.download_button(label="下载价格区间分析图表",data=buf,16 unmodified linesexcept Exception as e:st.error(f'处理文件时出错: {e}')else:st.info('请上传Excel文件以开始分析')8 unmodified lines91011121314151617181920212223242526272829303132333435363738394041424344454647484950173 unmodified lines2242252262272282292304 unmodified lines23523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529616 unmodified lines3133143153168 unmodified lines# 注册思源黑体字体font_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SourceHanSansSC-Normal-2.otf')# 创建字体属性对象chinese_font = fm.FontProperties(fname=font_path)# 设置全局字体属性plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题# 设置页面标题st.title('商品价格区间分析工具')# 添加使用说明with st.expander('使用说明', expanded=False):st.markdown("""### 使用说明本工具用于分析商品价格区间分布及销量占比情况,帮助您更好地理解产品在不同价格区间的销售情况。#### 使用步骤1. **上传数据**:点击下方的“上传商品数据Excel文件”按钮,选择您的Excel或CSV文件。- 文件必须包含以下列:“价格(¥)”、“月销量”和“月销售额(¥)”。2. **设置价格区间**:- 您可以选择自动使用数据中的最大最小价格,或勾选“手动设置价格区间范围”自定义范围。- 设置“价格区间大小”来决定每个价格段的跨度。3. **生成分析**:点击“生成价格区间分析”按钮,系统将自动计算并生成:- 价格区间分析结果表格- 价格区间分析图表- 各价格段销量占比横向柱状图4. **下载结果**:您可以下载以下格式的分析结果:- Excel表格- 价格区间分析图表(PNG格式)- 价格区间销量占比图表(PNG格式)""")# 上传文件功能uploaded_file = st.file_uploader("上传商品数据Excel文件", type=['xlsx', 'csv'])173 unmodified lines# 应用自定义字体到表格文本for (i, j), cell in table.get_celld().items():text = cell.get_text()text.set_fontproperties(chinese_font)# 保存图像到内存buf = io.BytesIO()4 unmodified linesst.subheader('价格区间分析图表')st.image(buf, use_container_width=True)# 创建横向柱状图展示各价格段销量占比st.subheader('价格区间销量占比分析')# 准备数据labels = [row['价格区间($)'] for row in result_data]# 从销量占比中提取数值(去掉百分号并转换为浮点数)quantity_percents = [float(row['销量占比'].replace('%', '')) for row in result_data]# 创建横向柱状图fig_bar, ax_bar = plt.subplots(figsize=(10, 8), dpi=120)# 绘制横向柱状图bars = ax_bar.barh(labels, quantity_percents, color='skyblue')# 添加数据标签for bar in bars:width = bar.get_width()label_text = ax_bar.text(width + 0.5, bar.get_y() + bar.get_height()/2, f'{width:.2f}%',ha='left', va='center', fontsize=10)# 设置标签文本的字体label_text.set_fontproperties(chinese_font)# 设置图表标题和标签title = ax_bar.set_title('各价格段销量占比', fontsize=16)xlabel = ax_bar.set_xlabel('销量占比(%)', fontsize=14)ylabel = ax_bar.set_ylabel('价格段', fontsize=14)# 应用中文字体到标题和标签title.set_fontproperties(chinese_font)xlabel.set_fontproperties(chinese_font)ylabel.set_fontproperties(chinese_font)# 应用中文字体到y轴刻度标签for label in ax_bar.get_yticklabels():label.set_fontproperties(chinese_font)# 调整布局plt.tight_layout()# 保存横向柱状图到内存bar_buf = io.BytesIO()plt.savefig(bar_buf, format='png', bbox_inches='tight', pad_inches=0.2)bar_buf.seek(0)# 显示横向柱状图st.image(bar_buf, use_container_width=True)# 提供柱状图下载链接st.download_button(label="下载价格区间销量占比图表",data=bar_buf,file_name="价格区间销量占比.png",mime="image/png")# 提供表格图下载链接st.download_button(label="下载价格区间分析图表",data=buf,16 unmodified linesexcept Exception as e:st.error(f'处理文件时出错: {e}')else:st.info('请上传Excel文件以开始分析')
app.py+88-7