How to call a method from a different blueprint in Flask?

2024/10/1 19:32:32

I have an application with multiple blueprinted modules.

I would like to call a method (a route) that would normally return a view or render a template, from within a different blueprint's route.

How can this be done correctly?

Thank you.

Answer

Views are just functions; import the function and call it directly, passing in any route parameters that it may have defined.

The role of the Blueprint is to make it easier to register a group of routes under a common prefix, group their templates and static resources, and handle request-related events for just that group (request started, request completed, etc.). But how you call a view doesn't change.

For example, if you have a route in the foo blueprint, in the foo.py module:

@foo.route('/bar/<id>')
def bar(id):return something_done_with_id(id)

you can import that function and use it elsewhere:

import foo@baz.route('/spam/ham/eggs'):
def baz():return foo.bar(42)

Here bar takes a parameter from the URL, named id, so when calling the view function we do need to pass in a value for that parameter.

Do note that any blueprint before_request, after_request and context_processor functions are not executed (that happens at routing time), nor are Blueprint-specific error handlers in effect.

https://en.xdnf.cn/q/70938.html

Related Q&A

Dynamically define functions with varying signature

What I want to accomplish:dct = {foo:0, bar:1, baz:2} def func(**dct):pass #function signature is now func(foo=0, bar=1, baz=2)However, the ** syntax is obviously clashing here between expanding a dict…

python pandas plot with uneven timeseries index (with count evenly distributed)

My dataframe has uneven time index.how could I find a way to plot the data, and local the index automatically? I searched here, and I know I can plot something like e.plot()but the time index (x axis)…

python OpenAI gym monitor creates json files in the recording directory

I am implementing value iteration on the gym CartPole-v0 environment and would like to record the video of the agents actions in a video file. I have been trying to implement this using the Monitor wra…

Install xgboost under python with 32-bit msys failing

Trying to install xgboost is failing..? The version is Anaconda 2.1.0 (64-bit) on Windows & enterprise. How do I proceed? I have been using R it seems its quite easy to install new package in R …

Pythons _winapi module

I was trying to write some python code that requires calls to native WINAPI functions. At first I came across the pypiwin32 package. Then, somewhere on the internet I saw someone using the _winapi modu…

list comprehension with numpy arrays - bad practice?

I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach. Here is the code in question:a = np.array([[1,2,3],[4,5,6…

pandas: write dataframe to excel file *object* (not file)?

I have a dataframe that I want to convert to excel file, and return it using HTTP. Dataframes to_excel method accepts either a path, or an ExcelWriter, which, in turn, refers to a path.Is there any way…

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?win32api.SetCursorPos((x,y))vs:nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemM…

Pandas: Unstacking One Column of a DataFrame

I want to unstack one column in my Pandas DataFrame. The DataFrame is indexed by the Date and I want to unstack the Country column so each Country is its own column. The current pandas DF looks like t…

python-polars split string column into many columns by delimiter

In pandas, the following code will split the string from col1 into many columns. is there a way to do this in polars? d = {col1: ["a/b/c/d", "a/b/c/d"]} df= pd.DataFrame(data=d) df…