Python Forum

Full Version: Simple flask rest api problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to write flask app for my project. It's meant to get photo from user, then induce openCV class that detects objects in the picture.

I've watched some tutorials, but none of them is similar to that and I'm really close to deadline. Wall

Please, help me with @app.route methods to POST picture and GET bool return from openCV class. Confused
What have you tried so far? Post your code in python tags, full traceback if you get any - in error tags and ask specific questions.
We are glad to help, but we are not going to do your homework for you.
Cat_Detector is other file where class is_cat is checking if there are any cats in the picture.

The upload part i took from flask documentation.

If frontend was written in ASP.net mvc, how i make it used in this? Frontend app has to use POST to upload image to this api, then frontend has to request GET for answer about number of cats in picture, so i need to prepare api for that.

Sorry if my code hurts your eyes, glad to hear that you are ready to help. I don't want this project to be done by someone else, I need to understand this for the future.

I hope that my questions are more specific now

import os

from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash
from Cat_Detector import is_cat
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = '/cats'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
def index():
    #something connected with frontend i guess, but found no examples of this
        return "test"

@app.route('/upload', methods=['POST'])
def upload():
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

@app.route('/cat', methods=['GET'])
def cat():
    #what should i put instead of questionmark?
    return is_cat(?)

if __name__ == "__main__":
    app.run(debug=True)
I need to make API in flask that has POST method to get photo from frontend and GET method which would give answer if any cats are in the picture. I've done something already but have no idea how to actually do it correctly.

I've took upload app.route from flask documentation. Cat_Detector is file with is_cat class in it, it checks for cats in the picture.

Any ideas? What to put instead of questionmark in cat route and in index? Is this code any good?

import os
 
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash
from Cat_Detector import is_cat
from werkzeug.utils import secure_filename
 
UPLOAD_FOLDER = '/cats'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
 
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
 
@app.route('/')
def index():
    #something connected with frontend i guess, but found no examples of this
        return "test"
 
@app.route('/upload', methods=['POST'])
def upload():
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
 
@app.route('/cat', methods=['GET'])
def cat():
    return is_cat(?)
 
if __name__ == "__main__":
    app.run(debug=True)
hi,
you can check the requests dictionary, and pass it to is_cat() from your directory.