初学者 Pythonanywhere 查询

Beginner Pythonanywhere Query

编辑 3:

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

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

基本上,用户会 'Enter an address: (form)',并且会显示 'name'、'shack'、'status' 和 'payments' 的值。

这是我目前的尝试 (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)

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

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

    return {'Name': client_db[ind]['Name'][0], 'Shack': client_db[ind]['Shack'][0], 'Status': client_db[ind]['Status'][0], 'Payments': client_db[ind]['Payments'][0]}

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

为了完成您的代码,此示例 return 是一个 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)