is there a way I can convert my python code to Api

Question:

I have my python code where am doing sentimental analysis I have created the api using flask but am new to it so am just fetching the api my code result problem is i want to also send user input to my api so that my api can pass that to my python code and give me the result Hope you understanding.

simple is i want to run api when api run it get my input than process it and give me the result

Asked By: Faiz Hassan

||

Answers:

To create an API in Python with Flask, we have to indicate: the endpoint, the method and the function that should be executed on that endpoint. Let’s see an example with an API that simply returns the text “Hello world!”. from flask import Flask, jsonify, request,send_file app = Flask() @app.

Answered By: Anshul Kumar

Used by NASA, ILM, Disney and hardware hackers, Python is a versatile programming language and an ideal choice for beginners. Whether you’re just creating a “Hello World” or a full-blown application, Python needs an interpreter and a bunch of supporting libraries to work. What if we could make a GUI application, all bundled inside of a single executable file?

Answered By: Navdeep Dhaliwal

There are many ways to accomplish what you’re looking for, but I’ll give you a couple basic examples here. The first way is to embed the inputted data as url parameters. You can use a GET request to do this. The other is to use a POST request where the request payload contains the input data.

Using a GET

The first step for this type of request is to set up the endpoint to pull the parameter value out of the url

from flask import request

@app.route('/get_example')
def get_example():
    input_data = request.args.get('input')
    input2_data = request.args.get('input2')

    # Then you process your data however you need to

    return {'input_1': input_data, 'input_2': input2_data} 

Then you would hit this endpoint with your data directly embedded in the url:

https://your_api.com/get_example?input_data=some_stuff&input_data2=some_other_stuff

This would return {"input_1":"some_stuff", "input_2":"some_other_stuff}

Using a POST

For more complex data, it might be better to use a POST request

from flask import request

# it's important to include POST as one of the permitted methods
@app.route('/post_example', methods = ['GET','POST'])
def post_example():
    # This returns a dictionary of the POST request payload
    post_data = request.get_json(force = True)

    post1_data = post_data['first_input']
    post2_data = post_data['second_input']


    return {'input_1': post1_data, 'input_2': post2_data} 

Then you can hit your endpoint using curl:

curl -d "first_input=some_stuff&second_input=some_other_stuff" -X POST https://your_api.com/post_example

And it would return {"input_1":"some_stuff", "input_2":"some_other_stuff}

Some References

How can I get the named parameters from a URL using Flask?

https://www.educative.io/answers/how-to-perform-a-post-request-using-curl

Answered By: Peter
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.