Reputation: 47
I am trying to add column of drop down select list to data frame before storing to mysql in FLASK, SQLAlchemy, Python. Can you help plz. Getting error:
TypeError: 'method' object is not subscriptable
Following are two files used for project. app.py and view_two.html. Basically, I am developing application to submit all bank statement excel file to mysql. First I use PANDAS to view data frame by converting df to dictionary. Now before sending to mysql I would like to assign categories to each transaction whether it is cleaning expense, bank fees, food expense etc..
app.py
@app.route('/submit_two', methods=['GET', 'POST'])
def submit_two():
category = (request.form.get['category'])
a_two = session.get('a_two')
df = pd.DataFrame(a_two)
df['Categories'] = category
new_data_two = df.to_dict(orient='record')
a_two = new_data_two
if request.method == 'POST':
# df.fillna(0)
# a_two.fillna(0)
for data in a_two:
fav_two = BankData(process_date=data.get('Process date'), description=data.get(
'Description'), debit=data.get('Debit'), credit=data.get('Credit'), balance=data.get('Balance'), category=data.get('Categories'))
db.session.add(fav_two)
db.session.commit()
print("Bank data submitted")
return render_template("index.html")
view_two.html
<body>
<!-- First Containter Starts -->
<div class="container">
<div class="row">
<div class="col md-12">
<div class="jumbotron p-3">
<!-- table header -->
<!-- Data view Table Starts -->
<form action="{{url_for('submit_two')}}" method="POST">
<table class="table table-hover table-dark">
{% if new_data_two %}
<tr>
{% for key in new_data_two[0] %}
<th> {{ key }} </th>
{% endfor %}
</tr>
{% endif %}
<!-- table rows -->
{% for dict_item in new_data_two %}
<tr>
{% for value in dict_item.values() %}
<td> {{ value }} </td>
{% endfor %}
<td>
<!-- <form action="{{url_for('submit_two')}}" method="POST"> -->
<select id="category" name="category">
<option value="bankFees">Bank Fees</option>
<option value="cleaning">Cleaning</option>
<option value="sundry">Sundry</option>
<option value="telephone">Telephone</option>
</select>
<!-- </form> -->
</td>
</tr>
{% endfor %}
</table>
<button class="btn btn-primary" type="submit" class="btn btn-success float-right btn-lg"
data-toggle="modal" data-target="{{url_for('submit_two')}}">Submit Bank Data</button>
</form>
<!-- Data View Table Ends -->
<!-- <div class="modal-body"> -->
<!-- <div class="jumbotron p-3"> -->
<!-- <form action="{{url_for('submit_two')}}" method="POST"> -->
<!-- <table> -->
<!-- <th> -->
<!-- <label> -->
<!-- <h2>Submit Bank data</h2> -->
<!-- </label> -->
<!-- <input type="file" class="form-control" name="myfile" required="1"> -->
<!-- </th> -->
<!-- <th> -->
<!-- <h2> -->
<!-- <button class="btn btn-primary" type="submit" -->
<!-- class="btn btn-success float-right btn-lg" data-toggle="modal" -->
<!-- data-target="{{url_for('submit_two')}}">Submit Bank Data</button> -->
<!-- </h2> -->
<!-- </th> -->
<!-- </table> -->
<!-- </form> -->
</div>
</div>
</div>
{%endblock%}
Upvotes: 0
Views: 408
Reputation: 2050
Your line:
category = (request.form.get['category'])
Should be: category = (request.form.get('category'))
Upvotes: 1