【问题标题】:Beginner Pythonanywhere Query初学者 Pythonanywhere 查询
【发布时间】:2016-11-09 06:45:06
【问题描述】:

编辑 3:

我正在尝试在 pythonanywhere 上显示来自 csv 文件的信息,从用户输入到表单中提示。

我已将 client_db.csv 加载到我在 pythonanywhere 上的文件中:'/home/myusername/mydirectory/client_db.csv'。

基本上,用户会“输入地址:(表单)”,然后会显示“姓名”、“小屋”、“状态”和“付款”的值。

这是我迄今为止的尝试 (v3),但我没有让它发挥作用。我怀疑 html 输入有问题?

from flask import Flask
import os
import sys
import csv
import numpy as np

app = Flask(__name__)

app.config["DEBUG"] = True

path = '/home/myusername/ishack'
if path not in sys.path:
    sys.path.append(path)

client_db = np.genfromtxt('/home/myusername/ishack/client_db.csv', delimiter=',',dtype=None, names=True)

@app.route('/')
def form()
    return """
        <html>
            <body>
                <h1>Enter a Shack Number</h1>

                    <form action="/address" method="POST">
                    <textarea class="form-control" name="address" placeholder="Enter a Shack Number"></textarea>
                    <input type="submit" />
                </form>
            </body>
        </html>
    """


@app.route('/address', methods=["POST"])
def display_info(address):
    ind = np.where(client_db['Shack']==address)[0]
    return {'Name': client_db['Name'][ind],
            'Shack': client_db['Shack'][ind],
            'Status': client_db['Status'][ind],
            'Payments': client_db['Payments'][ind]}

display_info(address)

【问题讨论】:

  • 那么,你试过用烧瓶写什么?请注意,您可能希望返回数据而不是打印数据
  • 顺便说一句,请确保使用 csv 的完整路径,例如 /home/myusername/myfolder/things.csv,而不仅仅是“things.csv”跨度>
  • 感谢 cmets - 将按照分步指南进行操作,如果尽快尝试则恢复。
  • @hwjp 好的,我完成了非常有帮助的教程。我在上面做了一个(初学者)尝试......请看看:)

标签: python flask pythonanywhere


【解决方案1】:

您在刚刚发布的代码中遇到了一些小问题:

  • 有一些小错误,例如缺少冒号等
  • 此外,请注意您错误地索引了矩阵,首先放置列,然后放置行,而事实恰恰相反。正确的句子是(例如注意 ind 在 Name 之前):

    return {'Name': client_db[ind]['Name'][0], 'Shack':client_db[ind]['Shack'][0], '状态':client_db [ind] ['状态'] [0], '付款':client_db[ind]['Payments'][0]}

  • 最后一个问题与表单的POST有关。要获取您必须使用的地址数据:address = request.form["address"]

为了完成您的代码,此示例返回一个 JSON 数据,其中包含 CSV 文件中的字段:

from flask import Flask, request, Response
from flask import request
import json
import os
import sys
import csv
import numpy as np

app = Flask(__name__)

app.config["DEBUG"] = True

path = '/home/myusername/ishack'
if path not in sys.path:
    sys.path.append(path)

client_db = np.genfromtxt('/home/myusername/ishack/client_db.csv', delimiter=',', dtype=None, names=True)


@app.route('/')
def form():
    return """
        <html>
            <body>
                <h1>Enter a Shack Number</h1>

                    <form action="/address" method="POST">
                    <textarea class="form-control" name="address" placeholder="Enter a Shack Number"></textarea>
                    <input type="submit" />
                </form>
            </body>
        </html>
    """


@app.route('/address', methods=["POST"])
def display_info():
    address = request.form["address"]
    ind = np.where(client_db['Shack'] == address)[0]
    res = {'Name': client_db[ind]['Name'][0],
            'Shack': client_db[ind]['Shack'][0],
            'Status': client_db[ind]['Status'][0],
            'Payments': client_db[ind]['Payments'][0]}
    return Response(json.dumps(res), mimetype='application/json')

app.run(host="0.0.0.0", port=5000)

【讨论】:

  • 再次感谢@maki。我已经使用了您的编辑,但仍然没有运气。很难看到发生了什么,因为 url 只是不断地给出一个“未处理的异常”错误。至少 html 代码应该显示一些东西?
猜你喜欢
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2015-12-09
  • 2021-09-08
相关资源
最近更新 更多