pythonbook/实例学习 nicegui/add point.py

38 lines
969 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import plotly.graph_objects as go
from nicegui import ui
import pandas as pd
from random import random
ui.label('登录系统')
x = ui.input(label='x:')
y = ui.input(label='y:')
z = ui.input(label='z:')
fig = go.Figure()
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
plot = ui.plotly(fig).classes('w-full h-40')
def add_trace():
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[x.value, y.value, z.value]))
plot.update()
ui.button('Add trace', on_click=add_trace)
df = pd.DataFrame(columns=['x', 'y', 'z'])
table = ui.table.from_pandas(df).classes('max-h-40')
def add_to_dataframe():
# 将新行添加到DataFrame
new_row = {'x': x.value, 'y': y.value, 'z': z.value}
global df # 使用global声明以便在函数内部修改全局变量df
df = df.append(new_row, ignore_index=True)
# 更新表格显示
table = ui.table.from_pandas(df).classes('max-h-40')
ui.button('Add to DataFrame', on_click=add_to_dataframe)
ui.run()