Search code examples
pythonflaskpythonanywhere

Beginner Pythonanywhere Query


Edit 3:

I am trying to display info from a csv file on pythonanywhere, prompted from a user input into a form.

I have loaded the client_db.csv onto my files on pythonanywhere here:'/home/myusername/mydirectory/client_db.csv'.

Essentially, the user would 'Enter an address: (form)', and values for 'name', 'shack', 'status' and 'payments' would display.

Here is my attempt so far (v3), but I am not getting it to work. I suspect there is something wrong with the html input?

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)

Solution

  • You are having minor problems in the code just posted:

    • There are minor errors like missing colons and so
    • Additionally, note you are indexing wrongly the matrix, putting first the column and later the row, when it is exactly the opposite. The correct sentence is (note ind is before Name for example):

      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]}

    • The last problem is related to the POST of the form. To grab the address data you must use: address = request.form["address"]

    For finishing your code, this example returns a JSON data with the fields found in the CSV file:

    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)