【问题标题】:Generate CSV file on button click in jinja2 template using python使用python在jinja2模板中单击按钮生成CSV文件
【发布时间】:2021-09-28 07:33:52
【问题描述】:

我有一个 jinja2 模板,它将在本地生成一个 html。 HTML 包含许多表格。当我们单击 html 中的提交按钮“转换为 csv”时,我想将这些表转换为 csv 文件。尝试以下代码时出现错误。

jinja2 模板 - high_conf_correct_tab1 和 low_conf_incorrect_tab1 来自包含表数据的 python 代码。

<form method='post'>
    <button type="submit" >Convert to CSV </button>
</form>

<br>
    <button id=high-conf-correct1 type="button" class="collapsible">Top 1 High Confidence Correct answer </button>
    <div class="content">
        {{high_conf_correct_tab1}}
    </div>
    <div>
        </br>  </br>
    </div>
    <button id=low-conf-incorrect1  type="button" class="collapsible">Top 1 Low Confidence Incorrect answer </button>
    <div class="content">
        {{low_conf_incorrect_tab1}}
    </div>

尝试过的python代码:

  url = "C:\\Users\\Docs\\html\\metrics.html"
    
    if request.method == 'POST':
       html = open(url).read()
       soup = BeautifulSoup(html)
       table = soup.select_one("table.data2_s")
       with open("out.csv", "w") as f:
                wr = csv.writer(f)
                wr.writerow(headers)
    f.close()

【问题讨论】:

    标签: python html pandas jinja2


    【解决方案1】:

    为此在html中使用以下表单配置,

    <form class="form-container" method="POST" action='/something.html'>
           <input type="submit" id="submit1" class='btn btn-success'>
    </form>
    

    'something.html' 是 django 后端的 url。

    【讨论】:

    • 这是给 jinja2 的。我们不会转向任何其他 html,但最终目标是将所有表格转换为 csv 格式。
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2017-04-10
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多