使用Python(Flask/Postgresql)进行数据表服务器端处理

2024-04-18 18:45:06 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我使用的是:python3,Flask,PostgreSQL,一个引导主题。在

我想做什么?

我有一张近34000个值的表。问题是,页面加载速度非常慢,原因是数值太多。如何使用Python和Flask的服务器端处理(或其他)来提高性能?在

代码:

这是我的一部分主.py公司名称:

@login_required
def home():
    connect() # Connect to the PG database
    connect.cur.execute("""SELECT * FROM test""")
    test_execute = connect.cur.fetchall()

    count_equipement()

    return render_template('index.html',
    value=test_execute, 
    value2=count_equipement.nb_equipement,
    value3=check_ok.nb_ok,
    value4=check_ko.nb_ko)

test_execute获取我的表的所有值。在我的索引.html以下是我如何显示数据:

^{pr2}$

在我的引导主题中,有一个.js来正确地对表进行分页。以下是8个值的结果:

Table

如何进行服务器端处理?我已经检查了this link,但我不认为我可以把这个应用到我的案例中。。。在


Tags: testflask主题executehtmlcheckcountconnect
1条回答
网友
1楼 · 发布于 2024-04-18 18:45:06

这是我问题的答案。(评论用法语)

@app.route('/stack', )
@app.route('/stack/<int:page>', defaults={'page': 1})
@login_required
def stack(page):
    """
        retourne la page 1 par défaut, puis change selon le numéro /4 par ex
    """
    connect()
    connect.cur.execute("""SELECT COUNT(*) FROM mydatabase""")
    count = connect.cur.fetchall()
    count = count[0][0]

    check.count()
    PER_PAGE = 10

    offset = ((int(page)-1) * PER_PAGE)
    connect.cur.execute("""SELECT *
                        FROM mydatabase
                        ORDER BY table1 OFFSET %s LIMIT %s""" % (offset, PER_PAGE))

    solutions = connect.cur.fetchall()

    # Display a 409 not found page for an out of bounds request
    if not solutions and page != 1:
        return(render_template('404.html', errmsg="Requested page out of bounds"), 404)
    pagination = Pagination(page, PER_PAGE, count)
    return(render_template('index.html',
                           r=request,
                           value=solutions,
                           pagination=pagination))


def url_for_other_page(page):
    """
    récupère l'url
    """
    args = request.view_args.copy()
    args['page'] = page
    return url_for(request.endpoint, **args)
app.jinja_env.globals['url_for_other_page'] = url_for_other_page

相关问题 更多 >