Python Dash App: Calculate Difference

 # Import necessary libraries

import dash

from dash import dcc, html

from dash.dependencies import Input, Output, State


# Initialize the Dash app

app = dash.Dash(__name__)


# Global default values

DEFAULT_VALUE1 = 1000

DEFAULT_VALUE2 = 400


# Function to calculate the difference between two values

def calculate_difference(value1, value2):

    return value1 - value2


# Define the layout of the app

app.layout = html.Div([

    html.Div([

        html.Label('Value1'),

        dcc.Input(

            id='input-box-1', 

            type='number', 

            value=DEFAULT_VALUE1, 

            placeholder='Enter first value'

        ),

    ]),

    html.Div([

        html.Label('Value2'),

        dcc.Input(

            id='input-box-2', 

            type='number', 

            value=DEFAULT_VALUE2, 

            placeholder='Enter second value'

        ),

    ]),

    html.Button('Update', id='update-button'),

    html.Div([

        html.Label('Difference:'),

        html.Div(id='difference-label', style={'display': 'inline-block'})

    ])

])


# Define the callback to update the output

@app.callback(

    Output('difference-label', 'children'),

    Input('update-button', 'n_clicks'),

    State('input-box-1', 'value'),

    State('input-box-2', 'value')

)

def update_output(n_clicks, value1, value2):

    if n_clicks is None:

        return f'{calculate_difference(DEFAULT_VALUE1, DEFAULT_VALUE2)}'

    difference = calculate_difference(float(value1), float(value2))

    return f'{difference}'


# Run the app

if __name__ == '__main__':

    app.run_server(debug=False)


Comments

Popular posts from this blog

host

Steps to create SSH key from git bash

test