-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app.py user-input image receive using POST and AJAX to prevent reload…
…ing after form submit, default event listener override
- Loading branch information
1 parent
729b61e
commit 12dea06
Showing
5 changed files
with
41 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
myenv/ | ||
myenv/ | ||
__pycache__/ |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
from flask import Flask, render_template, url_for | ||
|
||
from flask import Flask, render_template, url_for, request | ||
import os | ||
app = Flask(__name__) | ||
|
||
UPLOAD_FOLDER = os.path.join('static', 'uploads') | ||
@app.route('/') | ||
def index(): | ||
return render_template("index.html") | ||
|
||
@app.route('/recommendation') | ||
@app.route('/recommendation', methods = ['GET', 'POST']) | ||
def recommendation(): | ||
if request.method == 'POST': | ||
if 'image' in request.files: | ||
image = request.files['image'] | ||
image.save(os.path.join(UPLOAD_FOLDER, image.filename)) | ||
return render_template("recommendation.html") | ||
return render_template("recommendation.html") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
function upload() { | ||
const fileUploader = document.querySelector('#image-upload'); | ||
const image = fileUploader.files[0]; | ||
const fileReader = new FileReader(); | ||
fileReader.readAsDataURL(image); | ||
|
||
function upload(){ | ||
const fileUploader = document.querySelector('#image-upload') | ||
const image = fileUploader.files[0] | ||
const fileReader = new FileReader(); | ||
fileReader.readAsDataURL(image); | ||
|
||
fileReader.onload = (fileReaderEvent) => { | ||
fileReader.onload = (fileReaderEvent) => { | ||
const profilePicture = document.querySelector('.image-preview'); | ||
profilePicture.style.backgroundImage = `url(${fileReaderEvent.target.result})`; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Prevent the form from refreshing the page when submitting | ||
document.querySelector('form').addEventListener('submit', function(event) { | ||
event.preventDefault(); // Prevent the default form submission (page reload) | ||
|
||
const formData = new FormData(this); | ||
|
||
fetch('/recommendation', { | ||
method: 'POST', | ||
body: formData | ||
}) | ||
.then(response => response.text()) | ||
.then(html => { | ||
console.log("Form submitted successfully."); | ||
}) | ||
.catch(error => console.error('Error:', error)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters