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)
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)
Related
I am trying to build a website in python using flask that takes file uploads and saves them to a folder (called uploads). However, when two files with the same name are uploaded, the first one is overwritten by the last one. How can I prevent this in a way that means that I don't lose any files? Would adding a timestamp to the filename help or would there still be an issue if two files are uploaded at the same time?
Thanks!
Filesystem
FlaskProject:
├───static
├───templates
├───uploads
└───app.py
Html
{%extends "base.html" %}
{%block content%}
<p>
<h2>Upload Below!</h2>
<div></div>
<form action = "http://localhost:5000/upload_complete" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
{% endblock %}
Python
from flask import Flask, redirect, url_for, render_template, request
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "D:/Me/FlaskProject/uploads"
#app.route('/upload')
def upload():
return render_template("upload.html", page="Upload Images")
#app.route('/upload_complete', methods = ['GET', 'POST'])
def upload_complete():
if request.method == 'POST':
f = request.files['file']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
return redirect(url_for('upload_complete'))
return render_template("upload_complete.html", page="Upload Complete")
if __name__ == '__main__':
app.debug = True
app.run()
app.run(debug = True)
(upload_complete.html is just a thank you screen)
You can generate an uuid and update the filename with it.
You can generate uuid like this,
import uuid
... your code ..
hash_value = uuid.uuid4().hex
f.save(os.path.join(app.config['UPLOAD_FOLDER'], hash_value + secure_filename(f.filename)))
I prepared a script in which user can upload a file as an input file (text.dat) and then after importing it into a function (compute), another file as an output (server.log) can be downloaded by user.
But the script does not work properly - it requests for the input file but can not be downloaded at the end.
import os
from flask import Flask, request, render_template, url_for, redirect, send_file,
from compute import compute
app = Flask(__name__)
#app.route("/")
def fileFrontPage():
return render_template('fileform.html')
#app.route("/handleUpload", methods=['GET','POST'])
def handleFileUpload():
if 'photo' in request.files:
photo = request.files['photo']
if photo.filename != '':
photo.save(os.path.join('/home/Documents/web', photo.filename))
return redirect(url_for('fileFrontPage'))
#app.route('/download', methods = ['GET', 'POST'])
def tes():
with open("/home/Documents/web/download/server.log", "w") as fd, open('/home/Documents/web/text.dat', 'r') as out:
path = "server.log"
for i in out:
s = compute(float(i))
fd.write("{} \n".format(s))
return send_file('/home/Documents/web/download/server.log',as_attachment=True, attachment_filename='server.log')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
My html file as follows:
<html>
<head>
<title>Simple file upload using Python Flask</title>
</head>
<body>
<form action="/handleUpload" method="post" enctype="multipart/form-data">
Choose the file: <input type="file" name="photo"/><BR>
<input type="submit" value="Upload"/>
</form>
</body>
</html>
Change the route of tes() to something that doesn't conflict with fileFrontPage(), e.g. #app.route('/download')
I'm completely new to flask, and really am completely lost with how to approach this. I've looked into other SO questions but I can't seem to get this working regardless.
I have a form as such:
<form class="teamSelection" method="POST" action="/submitted">
<select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
<option disabled selected>Select a game</option>
<option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
</select>
<input class="btn" type="submit" value="submit">
</form>
and my flask as so:
from flask import Flask
app = Flask(__name__)
#app.route("/submitted")
def hello():
return "hello world"
The goal is to take the content of the selected/submitted dropdown item, pass this to the flask file where I then use the team names to scrape information about the match. However at the moment I can't even seem to get the POST of the form to work and am at a complete loss. I appreciate this is a pretty vague and open-ended question, but I seriously don't know how else to figure this out.
Should I instead use jquery to detect when the dropdown has changed and use AJAX to send a POST to somehow call the script and pass the values into it?
Any help would be greatly appreciated.
EDIT
I thought I put this in the original post, but must have forgot.
I am currently running an apache localhost server, and am working with flask via pycharm. All I've done at the moment is install the flask package in pycharm, and haven't set any of it up like I've seen in some tutorials do when running from the command line. I assumed this step wasn't necessary, as I already have a server up and running with apache?
When it comes to backend stuff like this I really have no idea, so apologies if that's a stupid assumption.
I've changed the flask to:
from flask import Flask
app = Flask(__name__)
#app.route("/submitted", methods=['POST'])
def hello():
with open("newTest.csv", mode="w+") as file:
fileWriter = csv.writer(file)
fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
file.close()
The reason being as I can see if this script is actually being called, if it is it will make a new csv file called newTest. After running the webpage and submitting no new csv file appears, so this script isn't being run, meaning it's likely due to me not configuring flask correctly?/The assumption that apache was enough was incorrect?
You have just to tell the flask method to accept POST request and to read parameters from the request
Example:
from flask import Flask, request
app = Flask(__name__)
#app.route("/submitted", methods=['POST'])
def hello():
myvariable = request.form.get("teamDropdown")
... your code ...
return "hello world"
So, your question is not about flask, but about fopen - you have to add a full file path including directory path script_dir = path.dirname(path.abspath(__file__)).
Flask script (modified for launching in my local copy of project):
from flask import Flask, render_template, request
import csv
from os import path
app = Flask(__name__)
script_dir = path.dirname(path.abspath(__file__))
#app.route ("/")
def index():
return render_template("index.html")
#app.route("/submitted", methods=["GET", "POST"])
def hello():
if request.method == "GET":
return render_template("index.html")
filefullpath = script_dir + '//newTest.csv'
with open(filefullpath, mode="w+") as file:
fileWriter = csv.writer(file)
fileWriter.writerow(['Time', 'HomeTeam', 'AwayTeam'])
file.close()
return "hello world"
index.html (in folder "/templates")
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Test
<br>
<form class="teamSelection" method="POST" action="/submitted">
<select class="teamForm" id="teamDropdownSelector" type="text" name="teamDropdown" placeholder="Select A Team">
<option disabled selected>Select a game</option>
<option id="WatfordVSManchester Utd" value="">Watford VS Manchester Utd</option>
</select>
<input class="btn" type="submit" value="submit">
</form>
</body>
</html>
Modify your code as:
from flask import Flask
app = Flask(__name__)
#app.route("/submitted", methods=['POST'])
def hello():
return request.form['teamDropdown']
Please let me know if that helps.
when i click on submit button i get an error which says:
"TypeError: 'Collection' object is not callable. If you meant to call the 'insert' method on a 'Database' object it is failing because no such method exists."
here is my signin.py code :
from flask import Flask, request, render_template
from pymongo import MongoClient
#app = Flask(__name__)
connection = MongoClient()
db = connection.project #database name.
collection = connection.signup # collection name.
#app.route('/signin/')
def index_show():
return render_template('signin.html')
#app.route('/signin/', methods = ['POST'])
def signup_form():
username = request.form['user']
passowrd = request.form['pass']
collection.insert({'user': username, 'passoword': passowrd})
if __name__ == '__main__':
app.run(debug = True)
my html file code is here :
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action=".">
<input type="text" name="user" /><br/><br/>
<input type="password" name="pass" /><br/><br/>
<input type="submit" name="submit" /><br/><br/>
</form>
</body>
The method has been deprecated and is changed to .insert_one() in the pymongo 3.x driver, there is also .insert_many() for multiple document creation:
collection.insert_one({'user': username, 'passoword': passowrd})
The .insert() method is now only supported in the 2.x and lower series.
I think, the root cause is in this line:
collection = connection.signup # collection name.
Contrary to the comment, you're getting a DB named signup. That should rather be:
collection = db.signup
please do make sure that you do this to an existing database,object has been returned successfully.
here is my code:
from pymongo import MongoClient
client=MongoClient()
db=client.testdb
new={"shopclues":1234,"rating":3}
result=db.testdb.insert_one(new)
result.inserted_id
ObjectId('59e2f0f2031b4b0b27ecfa09')
from flask.ext.wtf import Form
from flask import send_from_directory
from wtforms import StringField, BooleanField
from wtforms import SelectField
import os
from os import listdir
from os.path import isfile, join
crewPath = ("/myproject/app/static/Crews")
filenamesCrew = [f for f in listdir(crewPath) if isfile(join(crewPath,f)) ]
class userInput(Form):
json_fileCrew = SelectField(u"json_fileCrew", choices=[(f, f) for f in filenamesCrew])
def get_data(self):
json = send_from_directory (crewPath, self.json_fileCrew.data)
return json
#app.route('/CastCrew', methods=['GET', 'POST'])
def castCrew():
form = userInput(request.form["crewYear"])
return render_template('CastCrew.html', title = 'Cast Crew View', form = form)
#app.route("/data", methods=['GET', 'POST']) #the javascript will call this
def data():
form = userInput(request.form["crewYear"])
return form.get_data()
<form class = "form" action="/data" method="post" name="crewYear">
{{ form.hidden_tag() }} <!--CSFR config -->
<p>Please choose a year:<br>
{{ form.json_fileCrew }}<br></p>
<p><input type="submit" value="Submit"></p>
</form>
I am getting a "Bad Request" error when I submit the form. How do I fix this?
The layout of project files:
---app
---views.py
---forms.py
---static
---Crews (about 100 .json files in this folder)
---1981.json
---css
---js
---templates
---base.html
---crew.html
I modified code according answer below but I still get a 404 Not Found Error when I click the button.
The immediate problem is that you're passing the value of request.form["crewYear"] to your form, rather than the entire request.form.
There are a lot of minor problems with your code. You don't need to use send_from_directory, since there's a more specific function to send from the static directory. You should populate the select field in the form init, otherwise it will not reflect any files added after the app starts. You should use app.static_folder rather than hard coding the path. You have two routes that do the same thing.
import os
from flask import current_app
from flask_wtf import Form
from wtforms.field import SelectField
class CrewForm(Form):
filename = SelectField()
def __init__(self, *args, **kwargs):
root = os.path.join(current_app.static_folder, 'Crews')
choices = [(f, f) for f in os.listdir(root) if os.path.isfile(os.path.join(root, f))]
self.filename.kwargs['choices'] = choices
super(CrewForm, self).__init__(*args, **kwargs)
#app.route('/crew', methods=['GET', 'POST'])
def crew():
form = CrewForm()
if form.validate_on_submit():
return current_app.send_static_file('Crews/{}'.format(form.filename.data))
return render_template('crew.html', form=form)
<form method="post">
{{ form.hidden_tag() }}
{{ form.filename }}
<input type="submit" value="Get Data"/>
</form>
Consider reading the Flask tutorial and Flask-WTF docs, as they clearly explain how to use forms. Reading PEP 8 would also be beneficial, as your code style is very inconsistent.