From c9969f62fbbe263571ed1effb055f7c1bc4d1bc8 Mon Sep 17 00:00:00 2001 From: shao1chuan <4448445@qq.com> Date: Tue, 12 Mar 2024 21:03:05 +0800 Subject: [PATCH] shao --- 实例学习 nicegui/Plot events.py | 9 ++++ 实例学习 nicegui/add line.py | 15 ++++++ 实例学习 nicegui/add point.py | 27 ++++++++++ 实例学习 nicegui/show table.py | 19 +++++++ 实例学习 nicegui/登录系统.py | 23 +++++++++ 实例学习plotly/Express line.py | 9 +++- 实例学习plotly/go4.ipynb | 76 ++++++++++++++-------------- 实例学习plotly/go5.ipynb | 2 +- 实验 Pandas_速查表/groupby.py | 2 +- 9 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 实例学习 nicegui/Plot events.py create mode 100644 实例学习 nicegui/add line.py create mode 100644 实例学习 nicegui/add point.py create mode 100644 实例学习 nicegui/show table.py create mode 100644 实例学习 nicegui/登录系统.py diff --git a/实例学习 nicegui/Plot events.py b/实例学习 nicegui/Plot events.py new file mode 100644 index 0000000..78a03cd --- /dev/null +++ b/实例学习 nicegui/Plot events.py @@ -0,0 +1,9 @@ +import plotly.graph_objects as go +from nicegui import ui + +fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5])) +fig.update_layout(margin=dict(l=0, r=0, t=0, b=0)) +plot = ui.plotly(fig).classes('w-full h-40') +plot.on('plotly_click', ui.notify) + +ui.run() \ No newline at end of file diff --git a/实例学习 nicegui/add line.py b/实例学习 nicegui/add line.py new file mode 100644 index 0000000..d47bead --- /dev/null +++ b/实例学习 nicegui/add line.py @@ -0,0 +1,15 @@ +import plotly.graph_objects as go +from nicegui import ui +from random import random + +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=[random(), random(), random()])) + plot.update() + +ui.button('Add trace', on_click=add_trace) + +ui.run() \ No newline at end of file diff --git a/实例学习 nicegui/add point.py b/实例学习 nicegui/add point.py new file mode 100644 index 0000000..d942702 --- /dev/null +++ b/实例学习 nicegui/add point.py @@ -0,0 +1,27 @@ +import plotly.graph_objects as go +from nicegui import ui +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) + + + + + +ui.run() \ No newline at end of file diff --git a/实例学习 nicegui/show table.py b/实例学习 nicegui/show table.py new file mode 100644 index 0000000..55c79ef --- /dev/null +++ b/实例学习 nicegui/show table.py @@ -0,0 +1,19 @@ +from nicegui import ui +import pandas as pd + +# 创建一个简单的DataFrame +data = { + 'Name': ['Alice', 'Bob', 'Charlie'], + 'Age': [25, 30, 35], + 'City': ['New York', 'Paris', 'London'] +} +df = pd.DataFrame(data) + + +# 在nicegui中创建并显示表格 + +ui.label('显示DataFrame') +ui.table.from_pandas(df).classes('max-h-40') + +# 启动应用 +ui.run() diff --git a/实例学习 nicegui/登录系统.py b/实例学习 nicegui/登录系统.py new file mode 100644 index 0000000..d458eaa --- /dev/null +++ b/实例学习 nicegui/登录系统.py @@ -0,0 +1,23 @@ +from nicegui import ui + +# 设置正确的用户名和密码,仅用于演示目的 +CORRECT_USERNAME = 'user' +CORRECT_PASSWORD = 'password' + +# 定义登录函数 +def on_login(): + # 检查用户名和密码是否正确 + if username_input.value == CORRECT_USERNAME and password_input.value == CORRECT_PASSWORD: + login_status.text = '登录成功!' # 使用 .text 属性更新文本 + else: + login_status.text = '登录失败,请检查您的用户名和密码。' # 使用 .text 属性更新文本 + +# 创建UI元素 +ui.label('登录系统') +username_input = ui.input(label='用户名:') +password_input = ui.input(label='密码:') # 使用 type='password' 使密码输入不可见 +ui.button('登录', on_click=on_login) +login_status = ui.label('请进行登录') # 初始提示文本 + +# 启动应用 +ui.run() diff --git a/实例学习plotly/Express line.py b/实例学习plotly/Express line.py index ca24f5f..ed87f17 100644 --- a/实例学习plotly/Express line.py +++ b/实例学习plotly/Express line.py @@ -1,7 +1,14 @@ import plotly.express as px import pandas df = px.data.gapminder() -df1 = df.query("country == 'United States'") +df1 = df.query("country == 'United States' or continent == 'Asia'") + + +# 根据大陆对数据进行分组,并计算每个大陆的人均GDP平均值 +gdpPercap_summary = df.groupby('continent')[['gdpPercap','lifeExp']].mean().reset_index() + +# 显示结果 +print(gdpPercap_summary) print(df1.columns) fig = px.line(df1, diff --git a/实例学习plotly/go4.ipynb b/实例学习plotly/go4.ipynb index 26d88e6..f29f1dc 100644 --- a/实例学习plotly/go4.ipynb +++ b/实例学习plotly/go4.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "pycharm": { "name": "#%%\n" @@ -30,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "pycharm": { "name": "#%%\n" @@ -42,7 +42,7 @@ "text/plain": " 0 1 2 3 4 5 6 \n0 27.809850 49.61936 83.080670 116.66320 130.41400 150.72060 220.1871 \\\n1 27.719660 48.55022 65.213740 95.27666 116.99640 133.90560 152.3412 \n2 30.426700 33.47752 44.809530 62.47495 77.43523 104.21530 102.7393 \n3 16.665490 30.10860 39.969520 44.12225 59.57512 77.56929 106.8925 \n4 8.815617 18.35160 8.658275 27.58590 48.62691 60.18013 91.3286 \n\n 7 8 9 ... 14 15 16 17 \n0 156.1536 148.6416 203.7845 ... 49.96142 21.89279 17.02552 11.743170 \\\n1 151.9340 160.1139 179.5327 ... 33.08871 38.40972 44.24843 69.578600 \n2 137.0004 186.0706 219.3173 ... 48.47132 74.71461 60.09090 7.073525 \n3 166.5539 175.2381 185.2815 ... 60.55916 55.92124 15.17284 8.248324 \n4 145.7109 116.0653 106.2662 ... 47.42691 69.20731 44.95468 29.171970 \n\n 18 19 20 21 22 23 \n0 14.752260 13.667100 5.677561 3.312340 1.156517 -0.147662 \n1 4.019351 3.050024 3.039719 2.996142 2.967954 1.999594 \n2 6.089851 6.537450 6.666096 7.306965 5.736840 3.625628 \n3 36.680870 61.934130 20.268670 68.588190 46.498120 0.236010 \n4 17.916740 16.255150 14.655590 17.260480 31.222450 46.717040 \n\n[5 rows x 24 columns]", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0123456789...14151617181920212223
027.80985049.6193683.080670116.66320130.41400150.72060220.1871156.1536148.6416203.7845...49.9614221.8927917.0255211.74317014.75226013.6671005.6775613.3123401.156517-0.147662
127.71966048.5502265.21374095.27666116.99640133.90560152.3412151.9340160.1139179.5327...33.0887138.4097244.2484369.5786004.0193513.0500243.0397192.9961422.9679541.999594
230.42670033.4775244.80953062.4749577.43523104.21530102.7393137.0004186.0706219.3173...48.4713274.7146160.090907.0735256.0898516.5374506.6660967.3069655.7368403.625628
316.66549030.1086039.96952044.1222559.5751277.56929106.8925166.5539175.2381185.2815...60.5591655.9212415.172848.24832436.68087061.93413020.26867068.58819046.4981200.236010
48.81561718.351608.65827527.5859048.6269160.1801391.3286145.7109116.0653106.2662...47.4269169.2073144.9546829.17197017.91674016.25515014.65559017.26048031.22245046.717040
\n

5 rows × 24 columns

\n
" }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "pycharm": { "name": "#%%\n" @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "pycharm": { "name": "#%%\n" @@ -1571,7 +1571,7 @@ "plotlyServerURL": "https://plot.ly" } }, - "text/html": "
" + "text/html": "
" }, "metadata": {}, "output_type": "display_data" @@ -1596,7 +1596,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "pycharm": { "name": "#%%\n" @@ -1609,7 +1609,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "pycharm": { "name": "#%%\n" @@ -1623,7 +1623,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "pycharm": { "name": "#%%\n" @@ -1634,28 +1634,6 @@ "xv, yv = np.meshgrid(x, y)" ] }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "pycharm": { - "name": "#%%\n" - } - }, - "outputs": [ - { - "data": { - "text/plain": "array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])" - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "xv" - ] - }, { "cell_type": "code", "execution_count": 10, @@ -1667,7 +1645,7 @@ "outputs": [ { "data": { - "text/plain": "array([[-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5],\n [-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4],\n [-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3],\n [-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2],\n [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])" + "text/plain": "array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],\n [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])" }, "execution_count": 10, "metadata": {}, @@ -1675,7 +1653,7 @@ } ], "source": [ - "yv" + "xv" ] }, { @@ -1689,13 +1667,35 @@ "outputs": [ { "data": { - "text/plain": "array([[50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50],\n [41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],\n [34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],\n [29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],\n [26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],\n [25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25],\n [26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],\n [29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],\n [34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],\n [41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],\n [50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50]])" + "text/plain": "array([[-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5],\n [-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4],\n [-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3],\n [-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2],\n [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])" }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], + "source": [ + "yv" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": "array([[50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50],\n [41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],\n [34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],\n [29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],\n [26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],\n [25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25],\n [26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],\n [29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],\n [34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],\n [41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],\n [50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50]])" + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z = xv**2 + yv**2\n", "z" @@ -1703,7 +1703,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "pycharm": { "name": "#%%\n" @@ -2984,7 +2984,7 @@ "plotlyServerURL": "https://plot.ly" } }, - "text/html": "
" + "text/html": "
" }, "metadata": {}, "output_type": "display_data" @@ -2998,7 +2998,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "pycharm": { "name": "#%%\n" diff --git a/实例学习plotly/go5.ipynb b/实例学习plotly/go5.ipynb index 4bd5c55..8330c8d 100644 --- a/实例学习plotly/go5.ipynb +++ b/实例学习plotly/go5.ipynb @@ -71139,7 +71139,7 @@ "plotlyServerURL": "https://plot.ly" } }, - "text/html": "
" + "text/html": "
" }, "metadata": {}, "output_type": "display_data" diff --git a/实验 Pandas_速查表/groupby.py b/实验 Pandas_速查表/groupby.py index e1b2f29..b5f753d 100644 --- a/实验 Pandas_速查表/groupby.py +++ b/实验 Pandas_速查表/groupby.py @@ -9,7 +9,7 @@ data = { 'Sales': [100, 150, 120, 110, 130, 120] } df = pd.DataFrame(data) -grouped_df = df.groupby('Product')['Sales'].sum() +grouped_df = df.groupby(by = 'Product')['Sales'].sum() print(grouped_df) # 这个操作将按照Product列进行分组,并计算每个分组中Sales列的总和,从而得到每个产品的总销售额。 # 这意味着产品A的总销售额为350,而产品B的总销售额为380。通过使用groupby方法,我们能够轻松地按照Product列对数据进行分组,并计算每个分组的Sales列总和,从而得到每个产品在所有日期上的总销售额。这个例子展示了groupby方法在数据分析和处理中的强大功能和灵活性