熊猫风格的表托管在图图中

发布于 2025-01-21 08:21:01 字数 6417 浏览 0 评论 0原文

晚上好,

我用脚注从SQL查询中创建了“几个”样式的数据帧表,并想创建一个子图形图,以报表格式托管这些表。

表代码如下:

sql_query = pd.read_sql_query (sqlq, connection)
df = sql_query

df['Product_Group'].loc[df['Product_Group'] == 'Pizzas'] = '*Pizzas'
df['Product_Group'].loc[df['Product_Group'] == 'Tratorria'] = '**Tratorria'

df['Price'] = df['Price'].apply(lambda x : '{:,}'.format(x))+ ' EGP';
df['Quantity'] = df['Quantity'].apply(lambda x : "{:,}".format(x))+ ' Units';

df = df.style.set_caption((
                            f'''Quantity and Sales broken down by <u>Product Group</u>.<br>
                            The data is filtered on <u>Order Date</u> and <u>Branch Name</u>.<br>
                            The Branch Name filter keeps <strong>{branch[2]}</strong>.<br>
                            * Pizzas Includes: <strong>(Pizza Meat,Pizza Vegetables, Pizza Turkey, Pizza Seafood, Appetizers)</strong><br>
                            ** Trattoria Includes: <strong>(Pasta,Risotto)</strong>'''
                          ))

dfstyle = [dict(selector="th", props=[('font-size', '18px')]),\
           dict(selector="td", props=[('font-size', '16px')]),\
           dict(selector="caption",props=[("text-align", "left"),("caption-side", "bottom"),
                       ("font-size", "14px"),("color", 'black')]
               )
          ]
df = df.set_table_styles(dfstyle)
df = df.apply(bold_lastrow)
df = df.set_properties(subset=['Quantity','Price'], **{'width': '400px'})
df = df.set_properties(subset=['Product_Group'], **{'text-align': 'left','font-weight': 'bold'})
df = df.set_properties(**{'font-family': 'Century Gothic'})
df = df.hide_index()

html渲染以重新创建表:

'<style  type="text/css" >\n    #T_04faa_ th {\n          font-size: 18px;\n    }    #T_04faa_ td {\n          font-size: 16px;\n    }    #T_04faa_ caption {\n          text-align: left;\n          caption-side: bottom;\n          font-size: 14px;\n          color: black;\n    }#T_04faa_row0_col0,#T_04faa_row1_col0,#T_04faa_row2_col0,#T_04faa_row3_col0,#T_04faa_row4_col0{\n            text-align:  left;\n            font-weight:  bold;\n            font-family:  Century Gothic;\n        }#T_04faa_row0_col1,#T_04faa_row0_col2,#T_04faa_row1_col1,#T_04faa_row1_col2,#T_04faa_row2_col1,#T_04faa_row2_col2,#T_04faa_row3_col1,#T_04faa_row3_col2,#T_04faa_row4_col1,#T_04faa_row4_col2{\n            width:  400px;\n            font-family:  Century Gothic;\n        }#T_04faa_row5_col0{\n            font-weight:  bold;\n            text-align:  left;\n            font-weight:  bold;\n            font-family:  Century Gothic;\n        }#T_04faa_row5_col1,#T_04faa_row5_col2{\n            font-weight:  bold;\n            width:  400px;\n            font-family:  Century Gothic;\n        }</style><table id="T_04faa_" ><caption>Quantity and Sales broken down by <u>Product Group</u>.<br>\n                            The data is filtered on <u>Order Date</u> and <u>Branch Name</u>.<br>\n                            The Branch Name filter keeps <strong>All Branches</strong>.<br>\n                            * Pizzas Includes: <strong>(Pizza Meat,Pizza Vegetables, Pizza Turkey, Pizza Seafood, Appetizers)</strong><br>\n                            ** Trattoria Includes: <strong>(Pasta,Risotto)</strong></caption><thead>    <tr>        <th class="col_heading level0 col0" >Product_Group</th>        <th class="col_heading level0 col1" >Quantity</th>        <th class="col_heading level0 col2" >Price</th>    </tr></thead><tbody>\n                <tr>\n                                <td id="T_04faa_row0_col0" class="data row0 col0" >None</td>\n                        <td id="T_04faa_row0_col1" class="data row0 col1" >10.0 Units</td>\n                        <td id="T_04faa_row0_col2" class="data row0 col2" >250.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row1_col0" class="data row1 col0" >Beverages</td>\n                        <td id="T_04faa_row1_col1" class="data row1 col1" >301.0 Units</td>\n                        <td id="T_04faa_row1_col2" class="data row1 col2" >10,214.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row2_col0" class="data row2 col0" >Others</td>\n                        <td id="T_04faa_row2_col1" class="data row2 col1" >95.0 Units</td>\n                        <td id="T_04faa_row2_col2" class="data row2 col2" >7,315.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row3_col0" class="data row3 col0" >*Pizzas</td>\n                        <td id="T_04faa_row3_col1" class="data row3 col1" >144.0 Units</td>\n                        <td id="T_04faa_row3_col2" class="data row3 col2" >20,830.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row4_col0" class="data row4 col0" >**Tratorria</td>\n                        <td id="T_04faa_row4_col1" class="data row4 col1" >43.0 Units</td>\n                        <td id="T_04faa_row4_col2" class="data row4 col2" >6,715.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row5_col0" class="data row5 col0" >Grand Total</td>\n                        <td id="T_04faa_row5_col1" class="data row5 col1" >593.0 Units</td>\n                        <td id="T_04faa_row5_col2" class="data row5 col2" >45,324.0 EGP</td>\n            </tr>\n    </tbody></table>'

但是我仍然不知道如何在子图数字中托管此“造型对象”以将其导出为A4报告。

编辑2:

jupyter呈现样式的DF如下:

”“在此处输入图像说明”

但是,html html df.render()显示df如下:

​从继续进行嵌入输出HTML的研究以渲染报告。

Good evening,

I created "Several" styled DataFrame tables from SQL query with footnotes and would like to create a subplot figure to host these tables in a report format.

Table Code is as follows:

sql_query = pd.read_sql_query (sqlq, connection)
df = sql_query

df['Product_Group'].loc[df['Product_Group'] == 'Pizzas'] = '*Pizzas'
df['Product_Group'].loc[df['Product_Group'] == 'Tratorria'] = '**Tratorria'

df['Price'] = df['Price'].apply(lambda x : '{:,}'.format(x))+ ' EGP';
df['Quantity'] = df['Quantity'].apply(lambda x : "{:,}".format(x))+ ' Units';

df = df.style.set_caption((
                            f'''Quantity and Sales broken down by <u>Product Group</u>.<br>
                            The data is filtered on <u>Order Date</u> and <u>Branch Name</u>.<br>
                            The Branch Name filter keeps <strong>{branch[2]}</strong>.<br>
                            * Pizzas Includes: <strong>(Pizza Meat,Pizza Vegetables, Pizza Turkey, Pizza Seafood, Appetizers)</strong><br>
                            ** Trattoria Includes: <strong>(Pasta,Risotto)</strong>'''
                          ))

dfstyle = [dict(selector="th", props=[('font-size', '18px')]),\
           dict(selector="td", props=[('font-size', '16px')]),\
           dict(selector="caption",props=[("text-align", "left"),("caption-side", "bottom"),
                       ("font-size", "14px"),("color", 'black')]
               )
          ]
df = df.set_table_styles(dfstyle)
df = df.apply(bold_lastrow)
df = df.set_properties(subset=['Quantity','Price'], **{'width': '400px'})
df = df.set_properties(subset=['Product_Group'], **{'text-align': 'left','font-weight': 'bold'})
df = df.set_properties(**{'font-family': 'Century Gothic'})
df = df.hide_index()

HTML Render to recreate table:

'<style  type="text/css" >\n    #T_04faa_ th {\n          font-size: 18px;\n    }    #T_04faa_ td {\n          font-size: 16px;\n    }    #T_04faa_ caption {\n          text-align: left;\n          caption-side: bottom;\n          font-size: 14px;\n          color: black;\n    }#T_04faa_row0_col0,#T_04faa_row1_col0,#T_04faa_row2_col0,#T_04faa_row3_col0,#T_04faa_row4_col0{\n            text-align:  left;\n            font-weight:  bold;\n            font-family:  Century Gothic;\n        }#T_04faa_row0_col1,#T_04faa_row0_col2,#T_04faa_row1_col1,#T_04faa_row1_col2,#T_04faa_row2_col1,#T_04faa_row2_col2,#T_04faa_row3_col1,#T_04faa_row3_col2,#T_04faa_row4_col1,#T_04faa_row4_col2{\n            width:  400px;\n            font-family:  Century Gothic;\n        }#T_04faa_row5_col0{\n            font-weight:  bold;\n            text-align:  left;\n            font-weight:  bold;\n            font-family:  Century Gothic;\n        }#T_04faa_row5_col1,#T_04faa_row5_col2{\n            font-weight:  bold;\n            width:  400px;\n            font-family:  Century Gothic;\n        }</style><table id="T_04faa_" ><caption>Quantity and Sales broken down by <u>Product Group</u>.<br>\n                            The data is filtered on <u>Order Date</u> and <u>Branch Name</u>.<br>\n                            The Branch Name filter keeps <strong>All Branches</strong>.<br>\n                            * Pizzas Includes: <strong>(Pizza Meat,Pizza Vegetables, Pizza Turkey, Pizza Seafood, Appetizers)</strong><br>\n                            ** Trattoria Includes: <strong>(Pasta,Risotto)</strong></caption><thead>    <tr>        <th class="col_heading level0 col0" >Product_Group</th>        <th class="col_heading level0 col1" >Quantity</th>        <th class="col_heading level0 col2" >Price</th>    </tr></thead><tbody>\n                <tr>\n                                <td id="T_04faa_row0_col0" class="data row0 col0" >None</td>\n                        <td id="T_04faa_row0_col1" class="data row0 col1" >10.0 Units</td>\n                        <td id="T_04faa_row0_col2" class="data row0 col2" >250.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row1_col0" class="data row1 col0" >Beverages</td>\n                        <td id="T_04faa_row1_col1" class="data row1 col1" >301.0 Units</td>\n                        <td id="T_04faa_row1_col2" class="data row1 col2" >10,214.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row2_col0" class="data row2 col0" >Others</td>\n                        <td id="T_04faa_row2_col1" class="data row2 col1" >95.0 Units</td>\n                        <td id="T_04faa_row2_col2" class="data row2 col2" >7,315.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row3_col0" class="data row3 col0" >*Pizzas</td>\n                        <td id="T_04faa_row3_col1" class="data row3 col1" >144.0 Units</td>\n                        <td id="T_04faa_row3_col2" class="data row3 col2" >20,830.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row4_col0" class="data row4 col0" >**Tratorria</td>\n                        <td id="T_04faa_row4_col1" class="data row4 col1" >43.0 Units</td>\n                        <td id="T_04faa_row4_col2" class="data row4 col2" >6,715.0 EGP</td>\n            </tr>\n            <tr>\n                                <td id="T_04faa_row5_col0" class="data row5 col0" >Grand Total</td>\n                        <td id="T_04faa_row5_col1" class="data row5 col1" >593.0 Units</td>\n                        <td id="T_04faa_row5_col2" class="data row5 col2" >45,324.0 EGP</td>\n            </tr>\n    </tbody></table>'

Yet I still can't figure out how to host this "Styler Object" in a subplot figure to export as a A4 report.

EDIT 2:

Jupyter renders the styled DF as follows:

enter image description here

Yet HTML Render with df.render() displays the df as follows:

enter image description here

Which is obviously incorrect formatting thus refraining me from continuing to research embedding the output HTML to render the report.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文