,可以通过以下步骤实现:
pip install pandas
pip install dash
import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
data = {'Name': ['John', 'Mike', 'Sarah', 'Emma'],
'Age': [25, 30, 28, 35],
'City': ['New York', 'London', 'Paris', 'Tokyo']}
df = pd.DataFrame(data)
app.layout = html.Div([
dcc.Input(id='filter-input', type='text', placeholder='输入过滤条件'),
html.Table(id='table')
])
@app.callback(
Output('table', 'children'),
[Input('filter-input', 'value')]
)
def update_table(filter_value):
if filter_value:
filtered_df = df[df['Name'].str.contains(filter_value)]
else:
filtered_df = df
table = html.Table([
html.Thead(
html.Tr([html.Th(col) for col in filtered_df.columns])
),
html.Tbody([
html.Tr([
html.Td(filtered_df.iloc[i][col]) for col in filtered_df.columns
]) for i in range(len(filtered_df))
])
])
return table
if __name__ == '__main__':
app.run_server(debug=True)
这样,你就可以在浏览器中访问Dash应用,并在输入框中输入过滤条件,动态过滤显示符合条件的数据。
领取专属 10元无门槛券
手把手带您无忧上云