[Feature Request] PreProcess and PostProcess Callback Hooks
@BSd3v がすでに取り組んでいます。
2025年10月9日 から。
評価
この issue はまだ評価されていません。
説明
I'd like to request PreProcess and PostProcess hooks to be used within the scope of a callback.
Essentially, this would allow developers to intercept specific things before and after a callback function is called. The PostProcess would be placed before the response was sent back to the client.
This could potentially solve issues like this: https://github.com/plotly/dash/issues/262
Here is an example of how this could potentially work, though the hooks wouldnt be inside the callbacks:
import dash
from dash import html, Input, Output, dcc, ctx
import time
server_side_stores = {}
def my_preprocess(args, kwargs, input_state_list):
# Log and update args with server-side store data if available
for i in range(len(args)):
if 'id' in input_state_list[i]:
store_id = input_state_list[i]['id']
if store_id in server_side_stores:
args[i] = server_side_stores[store_id]
return args, kwargs
def my_postprocess(output_list, response):
# Log and update server-side store with response data
for i in range(len(output_list)):
if 'id' in output_list[i]:
store_id = output_list[i]['id']
server_side_stores[store_id] = response[i]
response[i] = {"timestamp": time.time()}
return response
class MyStore(dcc.Store):
def __init__(self, id, data, *args, **kwargs):
server_side_stores[id] = data
timestamped_data = {"timestamp": time.time()}
super().__init__(id=id, data=timestamped_data, *args, **kwargs)
app = dash.Dash(__name__, suppress_callback_exceptions=True)
app.layout = html.Div(
[
MyStore(id="my-store", data={"key": "value"}),
dcc.Input(id="input", type="text"),
html.Div(id="output"),
],
style={"padding": 50},
)
@app.callback(
Output("output", "children"),
Input("my-store", "data"),
)
def update_input(store_data):
# Preprocess
args, kwargs = my_preprocess(
[store_data], {}, ctx.inputs_list+ctx.states_list
)
result = f"Original data: {args[0]}, Store timestamp: {store_data['timestamp']}"
return result
@app.callback(
Output("my-store", "data"),
Input("input", "value"),
prevent_initial_call=True,
)
def update_store(input_value):
# Postprocess
resp = my_postprocess([ctx.outputs_list], [{"key": input_value}])
return resp[0]
if __name__ == "__main__":
app.run(debug=True)
I recommend adding the hooks to the _callback.py in _initialize_context after the func_args, func_kwargs are set and in _prepare_response. With the currently displayed pattern, there could be some potential performance drawbacks, so we might need to look at streamlining how these are iterated.
This could also be narrowed down to only the callbacks that need to be transformed/augmented in place.
This could also be used to allow dataframes to be returned from a callback, data validation on the frontend, etc.
- 主要言語
- Python
- スター
- 24.4k
- フォーク
- 2.3k
- 平均マージ
- 1日 21時間
- マージ済み PR(30日)
- 19
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
plotly/dash のほかの issue
-
good first issue P3 size: 1 task
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
enhancement P2 size: 5
難易度 5/5 1週間以上 初心者へのやさしさ 45/100
-
enhancement P3 size: 10+
-
P2 size: 1 task
-
enhancement P3 size: 1
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
essnmx good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
-
[Feature] 奇物选择添加优先级 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
syfoud/Simulated_Scepter#174 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Giskard-AI/giskard-oss#2840 · コメント 1 件 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success オープンarea: repo bug perceived difficulty: 2
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
yeti-platform/yeti#1380 ·