【发布时间】: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