【问题标题】:Retrieve value from Input field in Dash Framework从 Dash 框架中的输入字段中检索值
【发布时间】:2020-10-01 12:57:21
【问题描述】:

我是 Dash 框架的新手。请帮帮我。 我有输入字段,但我不知道如何检索用户输入的值。 当检索到该值时,我必须在我的 SQL 表中搜索该值,然后如果存在,则以表格格式显示具有该值的所有行。 我也不知道如何使用 SQL 来处理这些事情。

请帮忙。

这是我的代码 sn-p:

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import mysql.connector

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

cursor = db_connection.cursor()

df = pd.read_sql("select * from merge where ProductName=%s", db_connection,'Wine')

def generate_table(dataframe):
   return html.Table(className='hellob',
      # Header
      children=[html.Tr([html.Th(col) for col in dataframe.columns])] +
      # Body
      [html.Tr([
         html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
      ]) for i in range(len(dataframe))]
   )
    
app = dash.Dash()
app.layout = html.Div(children=[
   html.H4(children='Sales Of February 2020 by MHD'),

   html.Label('Product Name: '),
   dcc.Input(
       id='tfield',
       placeholder='Input the product name',
       type='text'
       ),
   generate_table(df)
])

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

现在代码正在运行,但是从我的数据库中提供整个表,我只需要 ProductName 与用户输入匹配的那些行。

   app.layout = html.Div(children=[
   html.H4(children='Sales Of February 2020 by MHD'),

   html.Label('Product Name: '),
   dcc.Input(
       id='tfield',
       placeholder='Input the product name',
       type='text'
       ),
   generate_table(df)
])

@app.callback(Input('tfield', 'value'))
def callback(input_value):
    return input_value

这也不适用于输入文本值 我也想存储和使用输入的值。

帮我解决这个问题。

【问题讨论】:

  • 帮帮我!

标签: mysql python-3.x database textfield plotly-dash


【解决方案1】:

我自己解决了我的问题 这是解决方案:

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

@app.callback(Output('my-output','children'),[Input('tfield', 'value')])
def callback(input_value):
   df = pd.read_sql("select * from merge where ProductName=%s", db_connection,params= 
        (input_value,))
    return generate_table(df)

【讨论】:

    【解决方案2】:

    发送参数的语法是你的情况

    df = pd.read_sql("select * from merge where ProductName=%s", db_connection, params=("Wine",))
    

    您必须过滤 Datafrane 以使用用户输入选择 olny

    df.filter(like='Chardonnay', axis=0)
    

    您输入破折号而不是“霞多丽”。

    每个元素都必须有它的唯一 id,所以你也应该把它添加到你的dcc.Input

    【讨论】:

    • 在哪里提到了葡萄酒,我希望该值来自代码中指定的输入字段。你能帮我吗?
    • 你必须在你的数据框中添加一个过滤器,在你插入文本的地方,like 也可以使用像Chard would also find Chardonnay as does nnay 这样的片段`
    • 这里这个过滤器不起作用,因为它不知道要在表的哪一列中搜索值。这里我需要在 ProductName 列中搜索值,还有一个问题是如何获取用户输入的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2018-12-25
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多