I am trying to load an image, based on what the user selects in a select box.
html:
<div class='image-container' id='image'>
<h3>Index: {{ photo_index }}</h3>
<h3>Filename: {{ image }}</h3>
<img src="{{ url_for('images.static', filename=image) }} " id="the-photo">
</div>
<div class='button-container' id='buttons'>
<form action="" method="post">
<input type="hidden" name="prev-next-buttons">
<input type="submit" value="Show prev photo" name='prev_photo'>
<input type="submit" value="Show next photo" name='next_photo'>
<br/>
<input type="submit" value="Show random photo" name='random_photo'>
<button type='button' id='rotate-button' onclick="rotateMe('#the-photo')">Rotate Photo</button>
</form>
<h3>Choose image from list:</h3>
<form method="post">
<input type="hidden" name="photo-select">
<select id="select-image" onfocus='this.size=5;' onblur='this.size=1' onchange="this.size=1; this.blur(); this.form.submit()">
{% for eimage in image_list %}
<option {% if eimage == image %} selected {% endif %}
href = "{{ url_for('main', chosen_image=eimage) }}"
>
{{eimage}}
</option>
{% endfor %}
</select>
</form>
</div>
routes.py
CUR_PHOTO_INDEX = 0
images = os.listdir(IMAGE_FOLDER)
image_urls = create_urls(images)
image_urls.append('favicon.ico')
num_images = len(image_urls) - 1
@app.route("/", methods=["GET", "POST"])
def main(chosen_image="Penguins.jpg"):
# if request.method == "POST":
global CUR_PHOTO_INDEX
if request.method == "POST":
if 'prev-next-buttons' in request.form:
CUR_PHOTO_INDEX = return_index(request.form, CUR_PHOTO_INDEX)
# print("Showing index", CUR_PHOTO_INDEX)
elif 'photo-select' in request.form:
CUR_PHOTO_INDEX = image_urls.index(chosen_image)
# print("\n", indx, "\n")
print("\n", chosen_image, CUR_PHOTO_INDEX, "\n")
return render_template('index.html',
title="Local Image Viewer",
photo_index=CUR_PHOTO_INDEX,
image=image_urls[CUR_PHOTO_INDEX],
image_list=image_urls)
What I'm expecting to happen, is when the user selects an option from the list, it sends that image name (a string, eimage
) to main()
, and then looks for that index in image_urls.index(chosen_image)
. But, whenever I do select an option, it just rpints the "Penguins.jpg" and its index over and over.
What am I overlooking to send the selected image name to the main()
function?