Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple flask rest api problem
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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)
Reply
#4
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)
Reply
#5
hi,
you can check the requests dictionary, and pass it to is_cat() from your directory.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  REST API using flask - limit connection? korenron 1 1,210 Feb-05-2023, 06:48 PM
Last Post: noisefloor
  Flask Rest API - fire and forget MorganSamage 2 1,534 Feb-04-2023, 11:09 AM
Last Post: MorganSamage
  Build a simple Webapp with Python Flask and mariaDB newbie1 3 3,317 Jun-04-2020, 09:34 PM
Last Post: lmolter54
  problem with the return of flask loutsi 4 2,054 Jun-04-2020, 08:12 AM
Last Post: loutsi
  Running simple flask - getting 404 beginner1 2 5,268 Oct-28-2019, 02:10 PM
Last Post: beginner1
  got some problem with flask signup form. darktitan 1 2,217 Aug-30-2019, 06:05 AM
Last Post: aligoren
  Flask rest api How to retrieve json request raysefo 4 6,014 Jan-20-2019, 06:46 PM
Last Post: raysefo
  Python Flask REST API vndywarhol 1 2,956 Sep-28-2018, 12:43 PM
Last Post: thomasp
  Problem With Simple Multiprocessing Script digitalmatic7 11 9,123 Apr-16-2018, 07:18 PM
Last Post: digitalmatic7
  Need Help with Simple Text Reformatting Problem MattTuck 5 3,711 Aug-14-2017, 10:07 PM
Last Post: MattTuck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020