python - 从 sqlite 为 pandas 数据框添加字幕并使用 Python 使用 Web 浏览器输出
问题描述
我的 Python 脚本中有一个函数,它从 sqlite 数据库中选择值到 Pandas (pd) 数据框中,然后在 Web 浏览器中输出。
我希望输出的表格在浏览器中显示表格的标题。
标题应该看起来像
“此表显示了年 year.get() 月份的收款率” year.get()
我的功能代码:
def all_collectors_info():
Database()
a = cursor.execute("SELECT * FROM `collectors` WHERE Month = ? AND Year = ?", (MONTH.get(), YEAR.get(),))
fetch = cursor.fetchall()
z = [x for x in fetch]
cols = [column[0] for column in a.description]
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
df= pd.DataFrame.from_records(data = fetch, columns = cols, index = list(range(1, (len(z)+1))))
html = df.to_html()
text_file = open("index.html", "w")
text_file.write(html)
text_file.close()
解决方案
没有直接选项可以将<caption>
标签从熊猫添加到表中,但您可以像这样附加标题(<h#> 标签)
heading_template = '<h3>This table shows the rate of collections for the month of {month} in the year {year}</h3>\n'
html = df.to_html()
html = heading_template.format(month=MONTH.get(), year=YEAR.get()) + html
所以你的最终功能是
def all_collectors_info():
Database()
a = cursor.execute("SELECT * FROM `collectors` WHERE Month = ? AND Year = ?", (MONTH.get(), YEAR.get(),))
fetch = cursor.fetchall()
z = [x for x in fetch]
cols = [column[0] for column in a.description]
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
df= pd.DataFrame.from_records(data = fetch, columns = cols, index = list(range(1, (len(z)+1))))
heading_template = '<h3>This table shows the rate of collections for the month of {month} in the year {year}</h3>\n'
html = df.to_html()
html = heading_template.format(month=MONTH.get(), year=YEAR.get()) + html
text_file = open("index.html", "w")
text_file.write(html)
text_file.close()
推荐阅读
- javascript - 像均匀的方形瓷砖一样绘制球体
- graphql - 如何从 apollo-link-error 获取 onError 回调中的 uri?
- r - 在 geom_bar 中插入文本
- java - 如何使用房间实体(使用类型转换器)来解析 Gson
- android - 为什么 PackageManager.queryIntentActivities 方法只返回 ACTION_VIEW 的默认浏览器信息?
- javascript - 有没有办法修改 FullCalendar 4 中的多日事件以显示为这样?
- python - 如何创建一个接受字典输入的函数?
- sql - oracle sql中唯一和重复的Sql查询?
- tensorflow - 如何从 .cfg 文件加载暗网 YOLOv3 模型并从 .weights 文件加载权重,并将带有权重的模型保存到 .h5 文件?
- ruby-on-rails - Rails 默认行为:为什么 Rails 默认不生成不同的 Etag 或返回 304 Not Modified?