Reputation: 51
@app.route("/upload_file", methods=["GET", "POST"])
def upload_file():
d={}
if request.method == 'POST':
df = pd.read_csv(request.files.get('file'), encoding="ISO-8859-1", engine="python")
links = df['live_url'].tolist()
for i in links:
for key,value in xml_data.items():
if i in value:
d.setdefault(key,[]).append(i)
res= d.items()
return render_template('results1.html', res= res, d=d)
return render_template('results1.html')
Hi there,
Please can someone be able to assist me on how to pass the below print output to result template. Thank you in advance.
Here is the below code for the result template. it takes input from search template and produce the result to result template:
search1.html
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="jumbotron" style="background:transparent !important">
<center>
<h2>Marketing Search</h2>
</center>
</div>
</div>
<form method="POST" action="/upload_file" enctype="multipart/form-data" style="margin:auto;max-width:25%;padding:50px;">
<p><input type="file" name="file" value="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body>
</html>
results.html
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="jumbotron" style="background:transparent !important"><center><h2>Marketing Search</h2></center>
</div></div>
<form class="example" action="/search/results" method="post" style="margin:auto;max-width:25%">
<input type="text" placeholder="Search.." name="input">
<button type="submit" class="btn"><i class="fa fa-search"></i></button>
</form>
<br/>
<center>
<h3>{{ res }} Results</h3>
</center>
<br/>
<table class="table">
<thead class="thead-dark">
<tr>
<th>Environment</th>
<th>Internal Name</th>
<th>Link</th>
</tr>
</thead>
<tbody>
{% for n,m in d.items() %}
<tr>
<td></td>
<td>{{n}}</td>
<td>{{m}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% block scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename='se.js') }}"></script>
{% endblock %}
</body>
</html>
Here is the above code for my templates.
Upvotes: 0
Views: 649
Reputation: 1627
I suggest you test the api POST request separately using Postman. You can select the csv file you want to upload there from your filesystem as form-data with "file" as a key, and your csv as the value. Dont forget to change the method to POST. Instead of rendering the html in this case, return res and d directly (as json), so Postman can display it.
If it works via Postman, you can rule out that the behaviour is broken in the backend, and then debug the error in the search1 or results htmls.
I think your dictionary is empty (when you use it in the results template) because it is already empty when it is passed to the template after upload (res and d). Also, I am not quite sure why you are using res and d separately.
Upvotes: 1