学习Flask之分页插件flask

这次分页功能,主要是依靠 Flask-Bootstrap

首先也是下载flask_bootstrap

pip install flask_bootstrap

安装完后可以观察里面的文件夹

里面其实还有nav(导航),form(表单),pagination(分页),table(表格),还有一些常用的模板代码,这次分页要使用pagination.html,可以打开看一下{# This file was part of Flask-Bootstrap and was modified under the terms of its BSD License. Copyright (c) 2013, Marc Brinkmann. All rights reserved. #}{% macro render_pager(pagination, fragment=”, prev=(‘←span> Previous’)|safe, next=(‘Next →span>’)|safe, align=”) -%} {{ prev }} a> li> {{ next }} a> li> ul> nav>{%- endmacro %}{% macro _arg_url_for(endpoint, base) %} {# calls url_for() with a given endpoint and **base as the parameters, additionally passing on all keyword_arguments (may overwrite existing ones) #} {%- with kargs = base.copy() -%} {%- do kargs.update(kwargs) -%} {{ url_for(endpoint, **kargs) }} {%- endwith %}{%- endmacro %}{% macro render_pagination(pagination, endpoint=None, prev=(”)|safe, size=None, ellipses=’…’, args={}, fragment=”, align=” )-%} {% if fragment != ” and not fragment.startswith(‘#’) %}{% set fragment = ‘#’ + fragment %}{% endif %} {% with url_args = {} %} {%- do url_args.update(request.view_args if not endpoint else {}), url_args.update(request.args if not endpoint else {}), url_args.update(args) -%} {% with endpoint = endpoint or request.endpoint %} {# prev and next are only show if a symbol has been passed. #} {% if prev != None -%} {{ prev }}a> li> {%- endif -%} {%- for page in pagination.iter_pages() %} {% if page %} {% if page != pagination.page %} {{ page }}a> li> {% else %} {{ page }} (current)span>a> li> {% endif %} {% elif ellipses != None %} {{ ellipses }}a>li> {% endif %} {%- endfor %} {% if next != None -%} {{ next }}a> li> {%- endif -%} ul> nav> {% endwith %} {% endwith %}{% endmacro %}

可以看到里面有三个 {% macro %},render_pager() _arg_url_for() render_pagination()(简单解释一下这个macro ,可以当作是一个模板方法,将公共的html 封装成一个方法,然后直接调用,如果是PHP 的框架,一般都是写在一个php 的公共的方法里面的,现在写在模板Html中,用法都差不多)大概看了一下这三个macro,render_pager()是个分页,但是只会显示”上一页”,”下一页”,_arg_url_for()是个路径连接的方法吧render_pagination() 也是个分页,不过会详细显示

好了,现在正式开始

在extendsions.py 仲调用

from flask_bootstrap import Bootstrap……bootstrap = Bootstrap()

在__init__.py 初始化

bootstrap.init_app(app)

在数据库查找中,利用 paginate

from epay.config import POST_PER_PAGEdef index(): page = request.args.get(‘page’, 1, type=int) pagination = Order.query.order_by(Order.created_at.desc()).paginate(page, per_page=POST_PER_PAGE) posts = pagination.items return render_template(‘admin/order/index.html’,pagination=pagination, posts=posts)

模板显示:

{% extends ‘admin/base.html’ %}{% from ‘bootstrap/pagination.html’ import render_pagination %}{% block content %} Invoice ( user invoice design )small>h2> div> div> div> #th> Statusth> Order Noth> Amountth> Emailth> Phoneth> Create Timeth> Actionth> tr> thead> {% if posts %} {% for v in posts %} {{ v.id }}td> {{ v.status }}td> {{ v.order_no }}td> {{ v.amount }}td> {{ v.email }}td> {{ v.phone }}td> {{ v.created_at }}td> i> a> i> a> td> tr> {% endfor %} {% endif %} tbody> table> {{ render_pagination(pagination) }}div> div> div> div> div> div> div>{% endblock content %}

最终的样式是这样:

PS:如果不喜欢bootstrap 的样式风格,其实可以自己写,自定义一个macro,仿照render_pagination() 重新写一个

Original: https://blog.csdn.net/winson20102010/article/details/119112204Author: 吴家健kenTitle: 学习Flask之分页插件flask_bootstrap

相关阅读Title: python 整合同类数据求分位值_【利用python进行数据分析】数据聚合与分组运算…

在数据集准备好之后,通常的任务是计算分组统计信息或生成数据透视表。

[En]

After the dataset is ready, the usual task is to calculate the grouping statistics or generate a PivotTable.

pandas提供了groupby功能,可以自然地对数据集进行切片、切块和摘要。

在本章中,我们将学习:

[En]

In this chapter, we will learn:

1根据一个或多个键(函数、数组或DataFrame列名)拆分pandas对象

2.计算分组摘要统计,如计数、平均值,标准差

3.对DataFrame的列应用各种各样的函数

4.计算透视表或交叉表

5.执行分位数分析以及其他分组分析

groupby技术——”split-apply-combine”(拆分-应用-合并)

分组键可以使用:

1.列表或数组

2.表示DataFrame某个列名的值

3.字典或Series,会与待分组轴的值进行一一对应

4.函数,用于处理轴索引或索引中的各个标签

如果按key1分组,可以:

如果一次传入多个分组数组,则按照分组数最多的结果显示,对应其他分组:

[En]

If you pass in an array of multiple groups at a time, it will be displayed according to the result of the largest number of groups and correspond to other groups:

可以使用unstack方法,将多重索引展开:

分组键也可以是任意长度的数组:

[En]

The grouping key can also be an array of any length:

也可以是DataFrame的列名:

如果列不是数字数据,则从结果中排除该列的聚合结果。

[En]

If a column is not numeric data, the aggregate result of that column is excluded from the result.

groupby有size方法,可以返回含有分组大小的Series:

对分组进行迭代:

其中,name将被赋值索引,groupby被赋值分组后的数值

一个有用的运算:将df.groupby(‘key1’)对象做成字典。

选取一个或一组列

只对data1和data2进行分组聚合。

通过字典或Series进行分组:

创建字典:

将字典传入groupby即可作为分组键

Series也有同样的功能,如果将Series作为索引,则pandas会检查Series以确保索引和分组轴是对齐的。

通过函数进行分组

如果想根据索引的长度进行分组,可以只传入获取长度的函数len:

将函数跟数组、列表、字典、Series混用也可以。

根据索引级别分组

层次化索引数据集能够根据索引级别进行聚合,只需要通过level关键字传入级别编号或名称即可:

hier_df.groupby(level=’cty’,axis=1).count()

数据聚合

GroupBy会高效地对Series进行切片,然后对各片调用piece.quantile(0.9),最后将这些结果组装成最终结果。

如果要使用自己对聚合函数,只需传入agg方法即可:

有些非聚合函数如describe也可以使用:

Original: https://blog.csdn.net/weixin_35452656/article/details/113966136Author: 小何小何Title: python 整合同类数据求分位值_【利用python进行数据分析】数据聚合与分组运算…

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/309434/

转载文章受原作者版权保护。转载请注明原作者出处!


比丘资源网 » 学习Flask之分页插件flask

发表回复

提供最优质的资源集合

立即查看 了解详情