[PYTHON] Get a Boolean in Flask's request

environment

First SOURCE

API that just receives and returns bool type ʻis_hoge` in request

hoge.py


from flask import Flask, request
app = Flask(__name__)

@app.route("/", methods=['GET'])
def hello():
    is_hoge = request.args.get('is_hoge', type=bool)
    return f'{ is_hoge }'

if __name__ == "__main__":
    app.run()

Expected value

--If you send true, True --If you send false, False --If you send 0, False

Execution result

--If you send true スクリーンショット 2019-11-19 15.21.06.png --If you send false スクリーンショット 2019-11-19 15.21.31.png --If you send 0 スクリーンショット 2019-11-19 15.21.55.png

What do you mean? As a result of all, True is returned. As a consideration of newcomer paper, I think that all of them were regarded as character strings and True was returned.

I want to be able to use it as a Boolean somehow

It seems that python has a convenient function called strtobool (), and I will use this.

converter.py


from distutils.util import strtobool

def convert_to_bool(target_object, default_val):
    if target_object is None:
        return default_val
    try:
        return strtobool(target_object)
    except:
        return default_val

When the value brought in as an argument is None or when an exception occurs, default_val is returned.

hoge.py


    is_hoge = converter.convert_to_bool(request.args.get('is_hoge'), False)

Run again

--If you send true スクリーンショット 2019-11-19 15.50.35.png

--If you send false スクリーンショット 2019-11-19 15.50.21.png

--If you send 0 スクリーンショット 2019-11-19 15.48.14.png

0s and 1s are now returned. If it is a judgment of the truth value of a numerical value, 0 is False, and other than 0 is True, so I think there is no problem!

Afterword

This is my first post, so I would appreciate it if you could let me know if there are any points that cannot be reached.

Recommended Posts

Get a Boolean in Flask's request
I get a KeyError in pyclustering.xmeans
How to get a stacktrace in python
Get a token for conoha in python
[Python] Get the files in a folder with Python
When I get a chromedriver error in Selenium
Get the caller of a function in Python
Get only the subclass elements in a list
Get a glimpse of machine learning in Python
Get a row containing a specific element in np.where
Get the file name in a folder using glob
Http request in python
I get a java.util.regex.PatternSyntaxException when splitting a string in PySpark
Flask Tutorial # 1 ~ GET Request ~
dict in dict Makes a dict a dict
BigQuery-If you get a Reason: responseTooLarge error in Python
About Go GET request
Get a participant's username and screen name in Slack
Developed a library to get Kindle collection list in Python
I tried "How to get a method decorated in Python"
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
I wrote a script to get a popular site in Japan
Call github api in python to get pull request information
Http request in python
String object methods in Python
Methods available in the list
Methods available in set type
Dynamically call methods in Python
Dynamically define functions (methods) in Python
Get a Boolean in Flask's request
Get YouTube Comments in Python
Take a screenshot in Python
Create a dictionary in Python
Get last month in python
Make a bookmarklet in Python
Draw a heart in Python
Get Evernote notes in Python
Get Japanese synonyms in Python
Try to get a list of breaking news threads in Python.
How to get all the possible values in a regular expression
What to do if you get a minus zero in Python
How to get a string from a command line argument in python
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
Get the formula in an excel file as a string in Python
Get the number of readers of a treatise on Mendeley in Python