当我使用破折号框架在文本框中输入值时,图形未显示 [英] Graph is not displaying when I enter the value in textbox using dash framework

查看:90
本文介绍了当我使用破折号框架在文本框中输入值时,图形未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表中的数据,当我返回dcc.Graph时,它什么也没有返回,只是在我面前显示了一个空白图

I have the data in a list and when I return the dcc.Graph then it doesn't return anything just a blank graph is displayed in front of me

让我向大家解释代码 在此代码中,我希望用户输入产品名称,如果产品名称存在于表中,并且瓶子列大于0,则选择那些行数据. 现在我们有一些行有月份和瓶子(数量) 这个月是重复的,因为一个产品在一个月内有多次使用不同的瓶子(数量),所以现在我要计算出所出售瓶子的独特月份数量 之后,我将唯一的月份列表和计算出的数量列表插入数据列表,如图所示 根据我的需要,此数据列表正确显示在输出中 但是之后,我希望将其绘制在条形图中,但对我不起作用. 所以在这里帮我.

Let me explain the code to you all In this code I want user to enter product name and if the product name is present in the table and the bottle column is greater than 0 then select those rows data. Now we have some rows having month and bottle(quantity) the month is repetitive as one product is there many times in one month with different bottle(quantity) so now I am calculating the unique months quantity of bottle sold After that I am inserting the unique month list and calculated quantity list to the data list as shown This data list is appearing correctly in the output as per my needs But after that I want it to be plotted in bar chart but its not working for me. So help me out here.

代码段:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
import pandas as pd
import mysql.connector

db_connection = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  db="trial"
)

cursor = db_connection.cursor()

app = dash.Dash()
app.layout = html.Div(children=[
   html.H4('Sales Of February 2020 by MHD'),
   html.Label('Product Name: '),
   dcc.Input(
       id='tfield',
       placeholder='Enter the product name',
       type='text'
       ),
   html.Br(),html.Br(),
   html.Div([dcc.Graph(id='my-output')])
])

@app.callback(Output('my-output','figure'),[Input('tfield', 'value')])
def callback(input_value):
    df = pd.read_sql("select Month,Bottle from merge where Bottle>0 and ProductName=%s", db_connection,params=(input_value,))
    mon=[]
    data=[]
    quan=[]
    uni_mon=[]
    uni_quan=[]
    y=0
    su=0
    
    for i in df.itertuples():
        mon.append(i.Month)
        quan.append(i.Bottle)
        
    uni_mon=list(set(mon))
    v=len(quan)
    
    for x in uni_mon:
        su=quan[y]
        while((y<v-1) and (mon[y]==mon[y+1])):
            su=su+quan[y+1]
            y=y+1
                
        y=y+1
        uni_quan.append(su)

    data.append({'x':uni_mon,'y':uni_quan,'type':'bar','name':input_value})
    print(data)
    figure={
        'data':data,
        'layout':{
            'title':'Graphhhh',
            'xaxis':'Month',
            'yaxis':'Quantity'
            }   
        }

    return figure

if __name__ == '__main__':
   app.run_server(debug=False)

所以shell上的结果是

so result on shell is

    Dash is running on http://127.0.0.1:8050/

 * Serving Flask app "dashnew" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
127.0.0.1 - - [09/Oct/2020 18:36:33] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [09/Oct/2020 18:36:33] "[37mGET /_dash-layout HTTP/1.1[0m" 200 -
127.0.0.1 - - [09/Oct/2020 18:36:33] "[37mGET /_dash-dependencies HTTP/1.1[0m" 200 -
[{'x': [], 'y': [], 'type': 'bar', 'name': None}]
127.0.0.1 - - [09/Oct/2020 18:36:33] "[37mPOST /_dash-update-component HTTP/1.1[0m" 200 -
[{'x': ['2月サマリー', '2月サマリー (Feb Sales)'], 'y': [1260, 4275], 'type': 'bar', 'name': 'M&C Brut Blan'}]
127.0.0.1 - - [09/Oct/2020 18:36:36] "[37mPOST /_dash-update-component HTTP/1.1[0m" 200 -

这是我输入的结果,服务器上未显示任何图形

this is my result when I enter and no graph is shown on server

请在这里帮助我,我已经更新了我的整个代码,并在执行该代码时收到了输出. 我想在浏览器中获取条形图,其中显示空白图形 无论我是否输入产品名称,它都会显示空白图表. 帮帮我!

please help me out here I have updated my entire code and output that I am receiving while executing this code. I want to get the bar chart in the browser where its showing blank graph Its displaying blank graph to me whether I enter a product name or not. Help me out!

推荐答案

我解决了这个问题:

figure={
    'data':data,
    'layout':{
        'title':'Graphhhh',
        'xaxis':{'title':'Month'},
        'yaxis':{'title':'Quantity'}
        }   
    }

这篇关于当我使用破折号框架在文本框中输入值时,图形未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆