Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Dash Dropdown

There are six questions to be asked to the user. If the user answers these questions, I am trying to make an application that will determine the result of which research design to use on the right. I am doing this application with python dash. My Python code is as follows. How do I write a python code that can bring the result of the research design according to the answers given after the user answers the questions? How should I write the callback code in the dash program for this? Thank you so much in advance.

Application screenshot in Turkish

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

from sympy import *

az = ""
bz = ""
app = dash.Dash(external_stylesheets=[dbc.themes.FLATLY])

app.config.suppress_callback_exceptions = True
logo = "analiz.png"

title = dcc.Link(id='ustbaslik',
                 href="/",
                 className="navbar-brand",
                 style={"margin-top": "12px"})

acıklama = [
    dbc.CardHeader(id='acik'),
    dbc.CardBody([
        html.H5(id='ortabaslik', className="card-title"),
        html.P(id='ortamesaj', className="card-text text-justify",),
    ]),
]

sonuc = [
    dbc.CardHeader(id='sonuc'),
    dbc.CardBody([
        html.P(id='mesaj', className="card-text text-justify",),
    ]),
]

app.layout = html.Div([
    html.Nav([
        dbc.Container([
            dbc.Row([
                dbc.Col(title, align="left",width="auto"),
                dbc.Col("", align="left",width="%100"),
                ],
                justify="between",
                align="center",
            ),
            dbc.Row([
                dbc.Col(
                    html.Div([
                        html.Button('Türkçe', id='btn-nclicks-1'),
                        html.Button('İngilizce', id='btn-nclicks-2'),
                    ]) 
                ),
                ],
                justify="between",
                align="center",
            )
        ])
        ],
        className="navbar navbar-dark bg-dark navbar-expand-md bg-light sticky-top",
    ),

    dbc.Container(
        dbc.Row([
            dbc.Col(dbc.Card(acıklama, color="primary", inverse=True)),
            dbc.Col(
                html.Div(children=[
                    html.Label('Araştırmadaki Değişken Türünü Giriniz:'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Nitel', 'value': 'nitel'},
                            {'label': 'Nicel', 'value': 'nicel'}
                        ],
                        value='tur'
                    ),
                    html.Hr(),
                    html.Label('Girişim var mı'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Evet', 'value': 'evet'},
                            {'label': 'Hayır', 'value': 'hayır'}
                        ],
                        value='gir'
                    ),
                    html.Hr(),
                    html.Label('Hipotez var mı:'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Evet', 'value': 'evet'},
                            {'label': 'Hayır', 'value': 'hayır'}
                        ],
                        value='hip'
                    ),
                    html.Hr(),
                    html.Label('Hasta sayısı:'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Bir hasta', 'value': 'bir'},
                            {'label': 'Birden fazla hasta', 'value': 'birden'},
                            {'label': 'Bir grup hasta', 'value': 'grup'}
                        ],
                        value='has'
                    ),
                    html.Hr(),
                    html.Label('Araştırma sorusunun yönü:'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Yok', 'value': 'yok'},
                            {'label': 'Geriye', 'value': 'geri'},
                            {'label': 'İleriye', 'value': 'ileri'}
                        ],
                        value='yon'
                    ),
                    html.Hr(),
                    html.Label('Araştırmadan elde edilen ölçüt:'),
                    dcc.Dropdown(
                        options=[
                            {'label': 'Prevelans Hızı', 'value': 'pre'},
                            {'label': 'Olasılık Oranı', 'value': 'ola'},
                            {'label': 'İnsidans Hızı', 'value': 'hız'}
                        ],
                        value='eld'
                    ),
                ])
                ),
                dbc.Col(dbc.Card(sonuc, color="primary", inverse=True)
            )],
            style={"margin-top": "50px"},
        ),
    ),
    html.Hr(),
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content'),
])

@app.callback([
    Output('ustbaslik', 'children'),
    Output('ortabaslik', 'children'),
    Output('ortamesaj', 'children'),
    Output('acik', 'children'),
    Output('sonuc', 'children')
],
[
    Input('btn-nclicks-1', 'n_clicks'),
    Input('btn-nclicks-2', 'n_clicks')
])

def displayClick(btn1, btn2):
    changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
    if 'btn-nclicks-1' in changed_id:
        ustbaslik = 'EPİDEMİYOLOJİK DENEY TASARIMLARI'
        ortabaslik = 'Epidemiyolojik Deney Tasarımları'
        ortamesaj = 'Epidemiyolojik araştırmalar, hastalıkların ...'
        acik = 'Açıklama'
        sonuc = 'Araştırma Tasarımınız ...'
    elif 'btn-nclicks-2' in changed_id:
        ustbaslik = 'EPIDEMIOLOGICAL EXPERIMENT DESIGN'
        ortabaslik = 'Theoretical Probability Distributions Software'
        ortamesaj = 'Epidemiological research, diseases and ...' 
        acik = 'Explanation'
        sonuc ='Your Research Design ...'
    else:
        ustbaslik = 'EPİDEMİYOLOJİK DENEY TASARIMLARI'
        ortabaslik = 'Epidemiyolojik Deney Tasarımları'
        ortamesaj = 'Epidemiyolojik araştırmalar, hastalıkların ...'
        acik = 'Açıklama'
        sonuc = 'Araştırma Tasarımınız ...'
    return ustbaslik,ortabaslik,ortamesaj,acik,sonuc

if __name__ == '__main__':
    app.run_server(debug=True)
like image 434
Hilbur Avatar asked Dec 07 '25 03:12

Hilbur


1 Answers

You need to make a couple of changes. Firstly you need to give your dropdown an id:

dcc.Dropdown(id='dropdown1',
            options=[{'label': 'Nitel', 'value': 'nitel'},
                     {'label': 'Nicel', 'value': 'nicel'}],
            value='tur'),

I've given it an id of 'dropdown1'

I also added a container for the output just to show the callback working:

html.Div(id='dd-output-container'),

Then the callback is simply:

@app.callback(
    Output('dd-output-container', 'children'),
    [Input('dropdown1', 'value')],
    )
def dropdown1_callback(v):
    return v

Hope this helps. More help can be found here

like image 58
Tom Avatar answered Dec 08 '25 18:12

Tom