Get query string as function parameters on flask

2024/10/12 16:21:32

Is there a way to get query string as function parameters on flask?
For example, the request will be like this.

http://localhost:5000/user?age=15&gender=Male

And hope the code similar to this.

@app.route("/user")
def getUser(age, gender):
...
Answer

If you are willing to write a decorator, anything is possible:

from functools import wrapsdef extract_args(*names, **names_and_processors):user_args = ([{"key": name} for name in names] +[{"key": key, "type": processor}for (key, processor) in names_and_processors.items()])def decorator(f):@wraps(f)def wrapper(*args, **kwargs):final_args, final_kwargs = args_from_request(user_args, args, kwargs)return f(*final_args, **final_kwargs)return wrapperreturn decorator if len(names) < 1 or not callable(names[0]) else decorator(names[0])def args_from_request(to_extract, provided_args, provided_kwargs):# Ignoring provided_* here - ideally, you'd merge them# in whatever way makes the most sense for your applicationresults = {}for arg in to_extract:result[arg["key"]] = request.args.get(**arg)return provided_args, results

Usage:

@app.route("/somewhere")
@extract_args("gender", age=int)
def somewhere(gender, age):return jsonify(gender=gender, age=age)
https://en.xdnf.cn/q/118185.html

Related Q&A

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…

Kivy class in .py and .kv interaction 2

Follow up from Kivy class in .py and .kv interaction , but more complex. Here is the full code of what Im writing: The data/screens/learnkanji_want.kv has how I want the code to be, but I dont fully un…

How to centre an image in pygame? [duplicate]

This question already has an answer here:How to center an image in the middle of the window in pygame?(1 answer)Closed 1 year ago.I am using python 2.7, I would like to know if there is a way to centr…

widget in sub-window update with real-time data in tkinter python

Ive tried using the after/time.sleep to update the treeview, but it is not working with the mainloop. My questions are: How can I update the treeview widget with real-time data? And is there a way th…

Changing for loop to while loop

Wondering how would the following for loop be changed to while loop. While having the same output.for i in range(0,20, 4):print(i)

How to dynamically resize label in kivy without size attribute

So, I get that you can usually just use self(=)(:)texture_size (py,kv) but all of my widgets are either based on screen(root only) or size_hint. I am doing this on purpose for a cross-platform GUI. I o…

How to parallelize this nested loop in Python that calls Abaqus

I have the nested loops below. How can i parallelize the outside loop so i can distribute the outside loop into 4 simultaneous runs and wait for all 4 runs to complete before moving on with the rest of…

Comparing one column value to all columns in linux enviroment

So I have two files , one VCF that looks like88 Chr1 25 C - 3 2 1 1 88 Chr1 88 A T 7 2 1 1 88 Chr1 92 A C 16 4 1 1and another with genes that looks likeGENEI…