gpt4 book ai didi

javascript - Python/Flask/HTML - 在新窗口而不是主页中呈现 HTML

转载 作者:行者123 更新时间:2023-11-27 22:57:24 25 4
gpt4 key购买 nike

我有一个 Python 代码,我在其中使用 Flask 创建网页。在主页上,我正在填写一个表单,使用一个按钮提交,它会根据输入显示一个表格。

我遇到的问题是,单击按钮提交表单后,它会在同一网页上呈现表格。我想使用 JavaScript 创建一个新窗口window.open() 或您可能建议的任何其他方法在新窗口中呈现该表并保持主页不变。我试着环顾四周,似乎什么也做不了。我已经通读了 this questionthis question .但这些建议似乎与我正在寻找的不符。

这是我的代码:

Python代码

from flask import Flask, render_template, request,
app = Flask(__name__)

def get_table(user_input):
...
return dict //returns list of dictionaries, for example...
//dict = [{'name':'Joe','age':'25'},
// {'name':'Mike','age':'20'},
// {'name':'Chris','age':'29'}]



@app.route("/")
def home():
return render_template('home.html')


@app.route("/table", methods = ['POST'])
def table():
user_input = request.form['input']
dict_table = get_table(user_input) //return list of dictionaries
return render_template('table.html', dict_table=dict_table)

if __name__ == '__main__':
app.run(debug=True)

home.html

<!DOCTYPE html>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<form action="/table" method="post">
<select name="input">
<option value="1">Input</option>
</select>
<button type="submit">Click Me!</button>
</form>
</body>
</html>

table.html

<!DOCTYPE html>
<html>
<head>
<title>Table</title>
</head>
<body>
<table id="table">
{% if dict_table %}
<tr>
{% for key in dict_table[0] %}
<th>{{ key }}</th>
{% endfor %}
</tr>
{% endif %}

{% for dict in dict_table %}
<tr>
{% for value in dict.values() %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>

谁能清楚地解释一下如何单击主页上的表单提交按钮,留在主页 home.html,并使 table.html 中的表格在新窗口中打开(可能使用来自 JavaScript 或其他东西的 window.open())?

如果有人可以引导我完成有关如何使用我提供的代码执行此操作的步骤,并具体告诉我在哪里调用函数和类似的东西,我将不胜感激。我是 Flask/HTML/JS 的新手,我只是为了个人使用而尝试学习,但我对阅读链接和文档感到沮丧,这些链接和文档显示了如何在新选项卡中显示像 google.com 这样的 URL,这不是我想要的。谢谢!

最佳答案

第一步:Check out this link that explains how to use Jquery and Ajax with FLASK

这里的关键概念是 AJAX(异步 JavaScript 和 XML)。简而言之,它是一种架构,可以在后台向服务器发送请求(称为异步请求),然后根据从服务器收到的结果修改 Web 浏览器当前显示的页面内容,避免 as以及服务器不再传输完整页面。

第 2 步:解决您的问题

  • 首先我们编写路由:

    from flask import Flask, render_template, request,
    app = Flask(__name__)

    user_input = None

    def get_table(user_input):
    ...
    return dict # returns list of dictionaries, for example...
    # dict = [{'name':'Joe','age':'25'},
    # {'name':'Mike','age':'20'},
    # {'name':'Chris','age':'29'}]

    @app.route('/')
    def home():
    return render_template('home.html')

    @app.route('/_ajax_user_input')
    def ajax_user_input():
    global user_input
    user_input = request.args.get('user_input', 0, type=int)
    return "ok"

    @app.route("/table")
    def table():
    x = user_input
    dict_table = get_table(x) # return list of dictionaries
    return render_template('table.html', dict_table=dict_table)
  • 在我们攻击模板之后:

  • home.html:

         <select id="input" name="input">
    <option value="1">Input</option>
    </select>
    <button type="button" class="test"> Click Me! </button>

    <script>
    $(document).ready(function(){
    $('.test').bind('click', function() {
    $.getJSON($SCRIPT_ROOT + '/_ajax_user_input',{
    user_input: $('#input').val(),
    },function() {
    window.open('http://127.0.0.1:5000/table', '_blank');
    });
    return false;
    });
    });
    </script>
  • table.html:

         <table id="table">
    {% if dict_table %}
    <tr>
    {% for key in dict_table[0] %}
    <th>{{ key }}</th>
    {% endfor %}
    </tr>
    {% endif %}

    {% for dict in dict_table %}
    <tr>
    {% for value in dict.values() %}
    <td>{{ value }}</td>
    {% endfor %}
    </tr>
    {% endfor %}
    </table>

基本上是这样的:

  • 当我点击我的按钮时,我调用 Javascript 脚本:

       $('.test').bind('click', function() {
  • 这会向 FLASK 发送一个 ajax 请求,包括执行 ajax_user_input() 函数:

       $.getJSON($SCRIPT_ROOT + '/_ajax_user_input',
  • 我向这个函数发送了一个数据(用户在 select 标签中选择的值),这个数据存储在变量 user_input 中:

       user_input: $('#input').val(),
  • 在 Flask 方面,我获取数据并将其存储在我也命名为 user_input 的全局变量中:

       global user_input
    user_input = request.args.get('user_input', 0, type=int)
  • 然后在我的脚本中调用一个 javascript 方法,允许我在新选项卡 ( more details here ) 中打开一个 url:

       window.open('http://127.0.0.1:5000/table', '_blank');
  • 'table' 路由,将之前存储在我的全局变量 (user_input) 中的数据存储在变量 x 中,然后调用函数 get_table()(通过在参数中传递变量 x)返回字典列表,最后返回页面 table.html,其中包含参数中的字典列表:

       x = user_input
    dict_table = get_table(x)
    return render_template('table.html', dict_table=dict_table)

我希望这会对你有所帮助,尽管我相信还有很多其他方法可以做到这一点,也许更有效。

关于javascript - Python/Flask/HTML - 在新窗口而不是主页中呈现 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54799041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com