[Python] displaying mean value using bar command and dash framework

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    my little project is following : we have a small database, in which information about car, their prices and country are presented, user selects any country and based on this one, mean value for given country is selected, here is my code :

    from sqlalchemy import create_engine,text
    #pip install pandas psycopg2-binary SQLAlchemy
    from dash import Dash, html, dcc, callback, Output, Input
    import plotly.express as px
    import pandas as pd
    app = Dash()

    # Requires Dash 2.17.0 or later
    app.layout = [
    html.H1(children='Title of Dash App', style={'textAlign':'center'}),
    dcc.Dropdown(["France","Germany","Italy"], 'France', id='dropdown-selection'),
    dcc.Graph(id='graph-content')
    ]

    @callback(
    Output('graph-content', 'figure'),
    Input('dropdown-selection', 'value')
    )
    def update_graph(value):
    country = (value,)
    engine = create_engine('postgresql+psycopg2://postgres:password@localhost/postgres')
    data = pd.read_sql(con=engine, sql='select * from CAR where country in (%s)', params=country)
    print(data)

    return px.bar(data_frame=data.groupby([value]).mean().reset_index(), x='price', y=value)

    if __name__ == '__main__':
    app.run(debug=True)






    # con =engine.connect()
    # con.execute(text('select * from leptop'))


    when i run code, i am getting following result : [​IMG]

    could you tell me please exactly where am i making mistake? please give me little hint

    Continue reading...

Compartilhe esta Página