Reputation: 572
I'm starting on Flask. I have the following view.py
My question is regarding the route @app.route('/uploaded'...
from flask import render_template, jsonify, Flask, redirect, url_for, request
from app import app
import random
import os
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
@app.route('/')
@app.route('/upload')
def upload_file2():
return render_template('index.html')
@app.route('/uploaded', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename)
model= ResNet50(weights='imagenet')
img = image.load_img(path, target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
preds_decoded = decode_predictions(preds, top=3)[0]
print(decode_predictions(preds, top=3)[0])
f.save(path)
return render_template('uploaded.html', title='Success', predictions=preds_decoded, user_image=f.filename)
@app.route('/index')
def index():
return render_template('index.html', title='Home')
@app.route('/map')
def map():
return render_template('map.html', title='Map')
@app.route('/map/refresh', methods=['POST'])
def map_refresh():
points = [(random.uniform(48.8434100, 48.8634100),
random.uniform(2.3388000, 2.3588000))
for _ in range(random.randint(2, 9))]
return jsonify({'points': points})
@app.route('/contact')
def contact():
return render_template('contact.html', title='Contact')
in the .html file, after some verification steps, I ask the End Under upload a file (an image).
Then in my code below, the input is the image and I want as output the results, that must be displayed in uploaded.
However, when I visit localhost:5000 I can't reach the uploaded.html
Is URL is stuck in my PHP request (the one that used to upload the file in my home.html)
I read many different answers from this site for similar problems, but none of them solve my issue.
Thanks in advance
index.html:
{% extends "layout.html" %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
<h1 class= "ui header" >{{ title }} </h1>
{% if current_user.is_authenticated %}
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<h2 class= "ui header" > Hi {{ title }} </h2>
{% endif %}
{% endblock %}
log:
* Debugger is active! * Debugger PIN: 127-013-588
127.0.0.1 - - [25/Sep/2019 22:12:18] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [25/Sep/2019 22:12:18] "GET /_debug_toolbar/static/css/toolbar.css?0.2942939835023174 HTTP/1.1" 200 -
127.0.0.1 - - [25/Sep/2019 22:12:26] "POST /upload.php HTTP/1.1" 404 -
Upvotes: 2
Views: 7052
Reputation: 51
Using a different port number worked for me! No idea why this worked as I have an alternative flask application that runs on port 8000. Thanks.
Upvotes: 1
Reputation: 2149
In your view.py, at the index level, try this:
@app.route('/index')
@app.route('/')
def index():
return render_template('index.html', title='Home')
You will be able to reach the site either by typing localhost:5000 or localhost:5000/index
Remove the route at the top (@app.route('/')
) as pointed out by @frankie567.
Upvotes: 1