Yes, you can create a Vue 3 web app as a frontend for your Python code to handle image uploads, processing, and downloads. However, to make this work, you’ll still need some sort of back-end server to process the uploaded images using your Python code. While this does introduce some server-related setup, it doesn't have to be overly complex. Here’s an outline of how you can structure this system:
Components of the System
-
Frontend (Vue 3 Web App):
- Allow users to upload images through a file input.
- Send the uploaded images to the back-end server.
- Provide feedback or a loading spinner while processing is happening.
- Display a download link once the processed image is available.
-
Backend (Python):
- Receive the uploaded images via an API.
- Process the images with your Python code.
- Serve the processed images back to the frontend for downloading.
-
Optional Lightweight Server:
- Use a lightweight server framework like Flask or FastAPI to bridge your Vue 3 app and the Python processing code.
- Handle file upload endpoints and serve processed files back to the client.
Steps to Implement This
Frontend (Vue 3):
- Use a file input field to allow users to upload images.
- Use
fetch or axios to send the files to your backend server.
- Display a loading spinner or message while waiting for a response.
- Provide a link or button to download the processed file once ready.
Example Vue.js component:
<template>
<div>
<input type="file" @change="handleFileUpload" />
<div v-if="isProcessing">Processing your image...</div>
<a v-if="downloadLink" :href="downloadLink" download="processed_image.png">Download Processed Image</a>
</div>
</template>
<script>
export default {
data() {
return {
isProcessing: false,
downloadLink: null,
};
},
methods: {
async handleFileUpload(event) {
const file = event.target.files[0];
if (!file) return;
this.isProcessing = true;
const formData = new FormData();
formData.append("file", file);
try {
const response = await fetch("http://localhost:5000/upload", {
method: "POST",
body: formData,
});
if (!response.ok) {
throw new Error("Failed to upload and process image");
}
// Assuming the server sends a link or the processed file back
const blob = await response.blob();
this.downloadLink = URL.createObjectURL(blob);
} catch (error) {
console.error(error);
alert("Error processing image.");
} finally {
this.isProcessing = false;
}
},
},
};
</script>
Backend (Python with Flask):
from flask import Flask, request, send_file
import os
from your_processing_code import process_image # Import your image processing function
app = Flask(__name__)
UPLOAD_FOLDER = "uploads"
PROCESSED_FOLDER = "processed"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(PROCESSED_FOLDER, exist_ok=True)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return "No file uploaded", 400
file = request.files['file']
if file.filename == '':
return "No selected file", 400
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
# Process the image
processed_filepath = os.path.join(PROCESSED_FOLDER, f"processed_{file.filename}")
process_image(filepath, processed_filepath) # Call your image processing function
return send_file(processed_filepath, as_attachment=True)
if __name__ == "__main__":
app.run(debug=True)
Optional Notes:
While there’s technically a server involved here, by utilizing a lightweight framework like Flask or FastAPI, you’re minimizing the complexity. These tools are intuitive and make it easy to set up the functionality you need.