You can download this code by clicking the button below.
This code is now available for download.
This function accepts two arguments and returns a random number within this range.
Technology Stack : Flask, Flask-Caching, random
Code Type : Flask Function
Code Difficulty : Intermediate
from flask import Flask, request, jsonify
from flask_caching import Cache
app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
@cache.cached(timeout=50, query_string=True)
def get_random_number(arg1, arg2):
# This function returns a random number between arg1 and arg2
return str(random.randint(arg1, arg2))
# JSON response with explanation
response_json = {
"type": "Flask Function",
"hard": "中级",
"explain": "这个函数接受两个参数,并返回一个在这个范围内的随机数。",
"tench": "Flask, Flask-Caching, random",
"explain_en": "This function accepts two arguments and returns a random number within this range.",
"tench_en": "Flask, Flask-Caching, random"
}
@app.route('/random_number')
def random_number():
return jsonify(response_json)
if __name__ == '__main__':
app.run(debug=True)