Dash में Callbacks

Dash और Plotly के साथ डैशबोर्ड बनाना

Alex Scriven

Data Scientist

Callbacks क्या हैं?

 

  • इंटरैक्शन से ट्रिगर हुई फ़ंक्शनैलिटी
    • एक यूज़र किसी एलिमेंट से इंटरैक्ट करता है
      • -> एक Python फ़ंक्शन ट्रिगर होता है
        • --> कुछ बदला जाता है

$$

$$

$$

  • क्यों? इंटरैक्टिविटी बढ़ती है ✨
Dash और Plotly के साथ डैशबोर्ड बनाना

Dash में Callbacks

  • डेकोरेटर फ़ंक्शन से शुरू करें
    • from dash import Input, Output उपयोग करता है
  • Output: फ़ंक्शन का रिटर्न कहाँ जाए
    • component_id: कंपोनेंट की पहचान करें
    • component_property: क्या बदलेगा
  • Input: क्या callback को ट्रिगर करेगा
    • component_property: ट्रिगर हुई फ़ंक्शन में क्या उपयोग होगा

 

@callback(

Output(component_id='my_plot', component_property='figure'),
Input(component_id='my_input', component_property='value')
)
def some_function(data): # Subset Data # Recreate Figure return fig
Dash और Plotly के साथ डैशबोर्ड बनाना

Dash में Dropdowns

 

dcc.Dropdown(id='title_dd',
             options=['Title 1', 'Title 2'])
  • वैल्यूज़ की सूची
Dash और Plotly के साथ डैशबोर्ड बनाना

एक dropdown callback

app.layout =[

dcc.Dropdown(id='title_dd', options=['Title 1', 'Title 2']),
dcc.Graph(id='my_graph')])
@callback( Output(component_id='my_graph', component_property='figure'),
Input(component_id='title_dd', component_property='value') )
# @callback()
def update_plot(selection):
    title = "None Selected"
    if selection:
        title = selection

bar_fig = px.bar( data_frame=ecom_sales, title=f'{title}', x='Total Sales ($)', y='Country')
return bar_fig
Dash और Plotly के साथ डैशबोर्ड बनाना

हमारा पहला dropdown

एक Chrome वेब ब्राउज़र का GIF जिसमें total sales (x-axis) और country (y-axis) का बार चार्ट है, ऊपर एक dropdown है जिसमें Title 1 और Title 2 विकल्प हैं. GIF में माउस हर विकल्प चुनता है और ग्राफ़ का शीर्षक उसी वैल्यू में बदल जाता है

Dash और Plotly के साथ डैशबोर्ड बनाना

फ़िल्टर के रूप में dropdown

  • कॉमन उपयोग: dropdown प्लॉट के DataFrame को फ़िल्टर करता है
# @callback()
def update_plot(input_country):
    country = 'All Countries'

sales = ecom_sales.copy(deep=True)
if input_country: country = input_country sales = sales[sales['Country'] == country]
bar_fig = px.bar( data_frame=sales, title=f"Sales in {country}", x='Total Sales ($)', y='Country') return bar_fig
Dash और Plotly के साथ डैशबोर्ड बनाना

एक्शन में फ़िल्टर

 

एक Chrome वेब ब्राउज़र का GIF जिसमें total sales (x-axis) और country (y-axis) का बार चार्ट है, ऊपर एक dropdown है जिसमें देशों के नाम विकल्प हैं. GIF में माउस अलग-अलग चुनता है, और ग्राफ़ का शीर्षक dropdown वैल्यू में बदलता है तथा चार्ट एक ही बार तक फ़िल्टर हो जाता है

Dash और Plotly के साथ डैशबोर्ड बनाना

अभ्यास करते हैं!

Dash और Plotly के साथ डैशबोर्ड बनाना

Preparing Video For Download...