系列微博目錄
Python微項(xiàng)目技術(shù)點(diǎn)案例示例系列
微博目錄
一、微項(xiàng)目開發(fā)背景和項(xiàng)目目標(biāo):
在學(xué)?;蚺嘤?xùn)班,教學(xué)管理頭緒繁雜,分析報(bào)告枯燥乏味。如果能編寫一個(gè)程序?qū)崿F(xiàn)數(shù)據(jù)可視化,界面圖形化,那就可以讓數(shù)據(jù)形象直觀生動(dòng)起來(lái),變得有趣生動(dòng),而且有靈魂。于是我靈感頓悟就有了寫一個(gè)數(shù)據(jù)可視化界面圖形化示例的想法。我打算使用Python的nicegui庫(kù)創(chuàng)建界圖形化界面面來(lái)進(jìn)行學(xué)生成績(jī)排名統(tǒng)計(jì)分析,使用Python數(shù)據(jù)可視化庫(kù)繪制圖表展示數(shù)據(jù),從而實(shí)現(xiàn)數(shù)據(jù)可視化示例代碼,并進(jìn)行逐步擴(kuò)展編程,完成比較完整完美示例代碼。
二、雛形示例代碼
下面是一個(gè)簡(jiǎn)單的示例代碼
import nicegui as ng
import matplotlib.pyplot as plt
# 模擬學(xué)生成績(jī)數(shù)據(jù)
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
grades = [85, 70, 90, 65, 75]
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)排名統(tǒng)計(jì)分析')
ng.plot(grades, title='學(xué)生成績(jī)折線圖', xlabel='學(xué)生', ylabel='成績(jī)')
# 繪制折線圖
plt.plot(students, grades)
plt.xlabel('學(xué)生')
plt.ylabel('成績(jī)')
plt.title('學(xué)生成績(jī)折線圖')
plt.show()
在這個(gè)示例中,我們首先模擬了一些學(xué)生某單一學(xué)科成績(jī)數(shù)據(jù)或總分成績(jī)數(shù)據(jù)或平均分成績(jī)數(shù)據(jù),然后使用nicegui庫(kù)創(chuàng)建了一個(gè)界面來(lái)展示學(xué)生成績(jī)的折線圖。在界面中,我們展示了學(xué)生成績(jī)的折線圖,并使用matplotlib庫(kù)來(lái)繪制了實(shí)際的折線圖。您可以根據(jù)需要對(duì)代碼進(jìn)行修改和擴(kuò)展,以滿足您的具體需求。
三、擴(kuò)展思路介紹
當(dāng)涉及到學(xué)生成績(jī)排名統(tǒng)計(jì)分析和折線圖展示的項(xiàng)目時(shí),可以進(jìn)一步擴(kuò)展思路來(lái)增強(qiáng)功能和用戶體驗(yàn)。以下是一些擴(kuò)展思路的介紹:
-
數(shù)據(jù)輸入:允許用戶輸入學(xué)生姓名和成績(jī),而不是使用預(yù)先定義的數(shù)據(jù)。這樣用戶可以自定義數(shù)據(jù)并進(jìn)行分析。
-
數(shù)據(jù)分析:除了展示折線圖外,可以添加其他圖表類型,如柱狀圖、餅圖等,來(lái)更全面地展示學(xué)生成績(jī)數(shù)據(jù)。
-
排名統(tǒng)計(jì):除了展示折線圖外,可以添加排名統(tǒng)計(jì)功能,顯示每個(gè)學(xué)生的排名情況,并提供排序功能。
-
數(shù)據(jù)導(dǎo)入導(dǎo)出:允許用戶導(dǎo)入數(shù)據(jù),并將分析結(jié)果導(dǎo)出為Excel、CSV等格式,以便進(jìn)一步處理或分享。
-
主題定制:提供主題定制功能,讓用戶可以選擇界面風(fēng)格和顏色,以增強(qiáng)用戶體驗(yàn)。
-
數(shù)據(jù)過濾:添加數(shù)據(jù)過濾功能,讓用戶可以根據(jù)特定條件篩選數(shù)據(jù)進(jìn)行分析。
-
數(shù)據(jù)比較:允許用戶選擇不同班級(jí)或?qū)W科的數(shù)據(jù)進(jìn)行比較分析,以便進(jìn)行更深入的學(xué)生成績(jī)分析。
8.優(yōu)化界面:讓界面更加漂亮美觀現(xiàn)代時(shí)尚。
通過以上擴(kuò)展思路,可以使項(xiàng)目更加全面和實(shí)用,提升用戶體驗(yàn),并為用戶提供更多的數(shù)據(jù)分析和展示功能。
四、數(shù)據(jù)輸入示例代碼
以下是一個(gè)示例代碼,演示如何使用Python的nicegui庫(kù)創(chuàng)建界面,允許用戶輸入學(xué)生姓名和成績(jī),并展示折線圖進(jìn)行分析:
import nicegui as ng
import matplotlib.pyplot as plt
# 初始化空列表用于存儲(chǔ)用戶輸入的數(shù)據(jù)
students = []
grades = []
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)排名統(tǒng)計(jì)分析')
# 添加輸入框,讓用戶輸入學(xué)生姓名和成績(jī)
student_name = ng.input('請(qǐng)輸入學(xué)生姓名:')
student_grade = ng.input('請(qǐng)輸入學(xué)生成績(jī):')
# 添加按鈕,用于提交數(shù)據(jù)
if ng.button('添加學(xué)生數(shù)據(jù)').clicked:
students.append(student_name.get())
grades.append(int(student_grade.get()))
# 添加按鈕,用于展示折線圖
if ng.button('展示折線圖').clicked:
ng.plot(grades, title='學(xué)生成績(jī)折線圖', xlabel='學(xué)生', ylabel='成績(jī)')
plt.plot(students, grades)
plt.xlabel('學(xué)生')
plt.ylabel('成績(jī)')
plt.title('學(xué)生成績(jī)折線圖')
plt.show()
在這個(gè)示例中,用戶可以通過輸入框輸入學(xué)生姓名和成績(jī),并通過按鈕提交數(shù)據(jù)。當(dāng)用戶點(diǎn)擊“展示折線圖”按鈕時(shí),程序?qū)⒄故居脩糨斎氲膶W(xué)生成績(jī)數(shù)據(jù)的折線圖。這樣用戶可以自定義數(shù)據(jù)并進(jìn)行分析。您可以根據(jù)需要對(duì)代碼進(jìn)行修改和擴(kuò)展,以滿足您的具體需求。
五、數(shù)據(jù)分析示例代碼
以下是一個(gè)示例代碼,演示如何使用Python的matplotlib庫(kù)創(chuàng)建不同類型的圖表,如柱狀圖和餅圖,來(lái)展示學(xué)生成績(jī)數(shù)據(jù):
import nicegui as ng
import matplotlib.pyplot as plt
# 初始化學(xué)生姓名和成績(jī)數(shù)據(jù)
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
grades = [85, 90, 78, 92, 88]
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)數(shù)據(jù)分析')
# 添加按鈕,用于展示柱狀圖
if ng.button('展示柱狀圖').clicked:
plt.bar(students, grades)
plt.xlabel('學(xué)生')
plt.ylabel('成績(jī)')
plt.title('學(xué)生成績(jī)柱狀圖')
plt.show()
# 添加按鈕,用于展示餅圖
if ng.button('展示餅圖').clicked:
plt.pie(grades, labels=students, autopct='%1.1f%%')
plt.title('學(xué)生成績(jī)餅圖')
plt.show()
在這個(gè)示例中,我們使用了matplotlib庫(kù)來(lái)創(chuàng)建柱狀圖和餅圖,展示了學(xué)生成績(jī)數(shù)據(jù)。用戶可以通過點(diǎn)擊不同的按鈕來(lái)查看不同類型的圖表。這樣用戶可以更全面地了解學(xué)生成績(jī)數(shù)據(jù)的分布情況。您可以根據(jù)需要對(duì)代碼進(jìn)行修改和擴(kuò)展,以滿足您的具體需求。
六、排名統(tǒng)計(jì)示例代碼
以下是一個(gè)示例代碼,演示如何添加排名統(tǒng)計(jì)功能,并提供排序功能,以顯示每個(gè)學(xué)生的排名情況:
import nicegui as ng
# 初始化學(xué)生姓名和成績(jī)數(shù)據(jù)
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
grades = [85, 90, 78, 92, 88]
# 計(jì)算每個(gè)學(xué)生的排名
ranked_students = sorted(zip(students, grades), key=lambda x: x[1], reverse=True)
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)排名統(tǒng)計(jì)')
# 添加按鈕,用于展示排名情況
if ng.button('顯示排名').clicked:
ng.text('學(xué)生排名情況:')
for i, (student, grade) in enumerate(ranked_students):
ng.text(f'{i+1}. {student}: {grade}')
# 添加按鈕,用于按成績(jī)排序
if ng.button('按成績(jī)排序').clicked:
ranked_students = sorted(zip(students, grades), key=lambda x: x[1], reverse=True)
ng.text('學(xué)生排名情況:')
for i, (student, grade) in enumerate(ranked_students):
ng.text(f'{i+1}. {student}: {grade}')
在這個(gè)示例中,我們首先計(jì)算了每個(gè)學(xué)生的排名,然后根據(jù)用戶的需求提供了兩個(gè)按鈕,一個(gè)用于顯示排名情況,另一個(gè)用于按成績(jī)排序并顯示排名情況。用戶可以通過點(diǎn)擊按鈕來(lái)查看學(xué)生的排名情況,并按照成績(jī)排序。這樣用戶可以更清晰地了解每個(gè)學(xué)生的排名情況。您可以根據(jù)需要對(duì)代碼進(jìn)行修改和擴(kuò)展,以滿足您的具體需求。
七、數(shù)據(jù)導(dǎo)入導(dǎo)出示例代碼
以下是一個(gè)重寫的示例代碼,演示如何允許用戶導(dǎo)入Excel或CSV格式的文件,并將分析結(jié)果導(dǎo)出為Excel或CSV格式的文件:
import nicegui as ng
import pandas as pd
# 初始化學(xué)生姓名和成績(jī)數(shù)據(jù)
students = []
grades = []
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)數(shù)據(jù)導(dǎo)入導(dǎo)出')
# 添加按鈕,用于導(dǎo)入數(shù)據(jù)
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
students = df['Student'].tolist()
grades = df['Grade'].tolist()
ng.text('數(shù)據(jù)導(dǎo)入成功!')
# 添加按鈕,用于展示數(shù)據(jù)
if ng.button('展示數(shù)據(jù)').clicked:
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade in zip(students, grades):
ng.text(f'{student}: {grade}')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為Excel文件
if ng.button('導(dǎo)出為Excel').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為CSV文件
if ng.button('導(dǎo)出為CSV').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
在這個(gè)示例中,我們根據(jù)用戶上傳的文件類型(CSV或Excel)選擇相應(yīng)的讀取方法。用戶可以上傳Excel或CSV格式的文件,并選擇導(dǎo)出為Excel或CSV格式的文件。這樣用戶可以根據(jù)需要靈活處理數(shù)據(jù)。您可以根據(jù)具體需求對(duì)代碼進(jìn)行修改和擴(kuò)展。
八、主題定制示例代碼
以下是更新后的示例代碼,添加了四種主題選項(xiàng):
import nicegui as ng
import pandas as pd
# 初始化學(xué)生姓名和成績(jī)數(shù)據(jù)
students = []
grades = []
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)數(shù)據(jù)導(dǎo)入導(dǎo)出')
# 添加主題定制功能
theme_options = ['Light', 'Dark', 'Blue', 'Green']
theme = ng.radio('選擇主題風(fēng)格', theme_options, default='Light')
if theme == 'Dark':
ng.set_theme('dark')
elif theme == 'Blue':
ng.set_theme('blue')
elif theme == 'Green':
ng.set_theme('green')
else:
ng.set_theme('light')
# 添加按鈕,用于導(dǎo)入數(shù)據(jù)
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
students = df['Student'].tolist()
grades = df['Grade'].tolist()
ng.text('數(shù)據(jù)導(dǎo)入成功!')
# 添加按鈕,用于展示數(shù)據(jù)
if ng.button('展示數(shù)據(jù)').clicked:
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade in zip(students, grades):
ng.text(f'{student}: {grade}')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為Excel文件
if ng.button('導(dǎo)出為Excel').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為CSV文件
if ng.button('導(dǎo)出為CSV').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
在這個(gè)示例中,我們添加了一個(gè)主題選擇器,讓用戶可以選擇界面風(fēng)格(Light或Dark)。根據(jù)用戶的選擇,我們調(diào)用ng.set_theme()函數(shù)來(lái)設(shè)置界面的主題風(fēng)格。用戶可以根據(jù)喜好選擇合適的主題風(fēng)格,以增強(qiáng)用戶體驗(yàn)。我們又?jǐn)U展示例添加了兩種額外的主題選項(xiàng)(Blue和Green),并相應(yīng)地設(shè)置界面的主題風(fēng)格。用戶現(xiàn)在可以選擇四種不同的主題風(fēng)格來(lái)定制界面。您可以根據(jù)需要進(jìn)一步擴(kuò)展和定制主題功能。希望這個(gè)示例對(duì)您有所幫助!如果您有任何問題,請(qǐng)隨時(shí)告訴我。
九、數(shù)據(jù)過濾示例代碼
以下是示例代碼,添加了數(shù)據(jù)過濾功能,讓用戶可以根據(jù)特定條件篩選數(shù)據(jù)進(jìn)行分析:
import nicegui as ng
import pandas as pd
# 初始化學(xué)生姓名和成績(jī)數(shù)據(jù)
students = []
grades = []
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)數(shù)據(jù)導(dǎo)入導(dǎo)出和過濾')
# 添加主題定制功能
theme_options = ['Light', 'Dark', 'Blue', 'Green']
theme = ng.radio('選擇主題風(fēng)格', theme_options, default='Light')
if theme == 'Dark':
ng.set_theme('dark')
elif theme == 'Blue':
ng.set_theme('blue')
elif theme == 'Green':
ng.set_theme('green')
else:
ng.set_theme('light')
# 添加按鈕,用于導(dǎo)入數(shù)據(jù)
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
students = df['Student'].tolist()
grades = df['Grade'].tolist()
ng.text('數(shù)據(jù)導(dǎo)入成功!')
# 添加按鈕,用于展示數(shù)據(jù)
if ng.button('展示數(shù)據(jù)').clicked:
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade in zip(students, grades):
ng.text(f'{student}: {grade}')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為Excel文件
if ng.button('導(dǎo)出為Excel').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為CSV文件
if ng.button('導(dǎo)出為CSV').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
# 添加數(shù)據(jù)過濾功能
ng.text('數(shù)據(jù)過濾:')
filter_value = ng.input('輸入過濾條件(成績(jī)大于等于多少):', type=float)
filtered_students = [student for student, grade in zip(students, grades) if grade >= filter_value]
ng.text('過濾后的學(xué)生成績(jī)數(shù)據(jù):')
for student in filtered_students:
ng.text(student)
在這個(gè)示例中,我們添加了一個(gè)數(shù)據(jù)過濾功能,讓用戶可以根據(jù)特定條件(成績(jī)大于等于用戶輸入的值)篩選數(shù)據(jù)進(jìn)行分析。用戶可以輸入過濾條件,并展示符合條件的學(xué)生姓名數(shù)據(jù)。您可以根據(jù)需要進(jìn)一步擴(kuò)展和定制數(shù)據(jù)過濾功能。
十、數(shù)據(jù)比較示例代碼
以下是示例代碼,添加了數(shù)據(jù)比較功能,允許用戶選擇不同班級(jí)或?qū)W科的數(shù)據(jù)進(jìn)行比較分析:
import nicegui as ng
import pandas as pd
# 初始化學(xué)生姓名、成績(jī)和班級(jí)數(shù)據(jù)
students = []
grades = []
classes = []
# 創(chuàng)建界面
with ng.box():
ng.text('學(xué)生成績(jī)數(shù)據(jù)導(dǎo)入導(dǎo)出、過濾和比較')
# 添加主題定制功能
theme_options = ['Light', 'Dark', 'Blue', 'Green']
theme = ng.radio('選擇主題風(fēng)格', theme_options, default='Light')
if theme == 'Dark':
ng.set_theme('dark')
elif theme == 'Blue':
ng.set_theme('blue')
elif theme == 'Green':
ng.set_theme('green')
else:
ng.set_theme('light')
# 添加按鈕,用于導(dǎo)入數(shù)據(jù)
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
students = df['Student'].tolist()
grades = df['Grade'].tolist()
classes = df['Class'].tolist()
ng.text('數(shù)據(jù)導(dǎo)入成功!')
# 添加按鈕,用于展示數(shù)據(jù)
if ng.button('展示數(shù)據(jù)').clicked:
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade, class_ in zip(students, grades, classes):
ng.text(f'{student}: {grade} - {class_}')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為Excel文件
if ng.button('導(dǎo)出為Excel').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades, 'Class': classes})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為CSV文件
if ng.button('導(dǎo)出為CSV').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades, 'Class': classes})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
# 添加數(shù)據(jù)過濾功能
ng.text('數(shù)據(jù)過濾:')
filter_value = ng.input('輸入過濾條件(成績(jī)大于等于多少):', type=float)
filtered_students = [student for student, grade in zip(students, grades) if grade >= filter_value]
ng.text('過濾后的學(xué)生成績(jī)數(shù)據(jù):')
for student in filtered_students:
ng.text(student)
# 添加數(shù)據(jù)比較功能
ng.text('數(shù)據(jù)比較:')
compare_option = ng.select('選擇比較對(duì)象', ['班級(jí)', '學(xué)科'])
if compare_option == '班級(jí)':
class_options = list(set(classes))
selected_class = ng.select('選擇班級(jí)', class_options)
class_students = [student for student, class_ in zip(students, classes) if class_ == selected_class]
ng.text(f'班級(jí)為{selected_class}的學(xué)生成績(jī)數(shù)據(jù):')
for student in class_students:
ng.text(student)
elif compare_option == '學(xué)科':
# 在這里添加學(xué)科比較邏輯
ng.text('學(xué)科比較功能暫未實(shí)現(xiàn)')
在這個(gè)示例中,我們添加了一個(gè)數(shù)據(jù)比較功能,允許用戶選擇不同班級(jí)或?qū)W科的數(shù)據(jù)進(jìn)行比較分析。用戶可以選擇比較對(duì)象(班級(jí)或?qū)W科),然后選擇具體的班級(jí)或?qū)W科進(jìn)行比較。您可以根據(jù)需要進(jìn)一步擴(kuò)展和完善學(xué)科比較功能。
十一、界面優(yōu)化示例代碼
以下是優(yōu)化后的示例代碼,讓圖形化界面更加漂亮美觀現(xiàn)代時(shí)尚:
import nicegui as ng
import pandas as pd
import plotly.express as px
# 初始化學(xué)生姓名、成績(jī)和班級(jí)數(shù)據(jù)
students = []
grades = []
classes = []
# 創(chuàng)建界面
with ng.box():
ng.title('學(xué)生成績(jī)分析')
# 添加按鈕,用于導(dǎo)入數(shù)據(jù)
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
students = df['Student'].tolist()
grades = df['Grade'].tolist()
classes = df['Class'].tolist()
ng.text('數(shù)據(jù)導(dǎo)入成功!')
# 添加按鈕,用于展示數(shù)據(jù)
if ng.button('展示數(shù)據(jù)').clicked:
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade, class_ in zip(students, grades, classes):
ng.text(f'{student}: {grade} - {class_}')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為Excel文件
if ng.button('導(dǎo)出為Excel').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades, 'Class': classes})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
# 添加按鈕,用于導(dǎo)出數(shù)據(jù)為CSV文件
if ng.button('導(dǎo)出為CSV').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades, 'Class': classes})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
# 添加數(shù)據(jù)過濾功能
ng.section('數(shù)據(jù)過濾')
filter_value = ng.input('輸入過濾條件(成績(jī)大于等于多少):', type=float)
filtered_students = [student for student, grade in zip(students, grades) if grade >= filter_value]
ng.text('過濾后的學(xué)生成績(jī)數(shù)據(jù):')
for student in filtered_students:
ng.text(student)
# 添加數(shù)據(jù)比較功能
ng.section('數(shù)據(jù)比較')
compare_option = ng.select('選擇比較對(duì)象', ['班級(jí)', '學(xué)科'])
if compare_option == '班級(jí)':
class_options = list(set(classes))
selected_class = ng.select('選擇班級(jí)', class_options)
class_students = [student for student, class_ in zip(students, classes) if class_ == selected_class]
ng.text(f'班級(jí)為{selected_class}的學(xué)生成績(jī)數(shù)據(jù):')
for student in class_students:
ng.text(student)
elif compare_option == '學(xué)科':
# 在這里添加學(xué)科比較邏輯
ng.text('學(xué)科比較功能暫未實(shí)現(xiàn)')
# 添加數(shù)據(jù)可視化圖表
ng.section('數(shù)據(jù)可視化')
if ng.button('顯示成績(jī)分布圖').clicked:
df = pd.DataFrame({'Student': students, 'Grade': grades, 'Class': classes})
fig = px.histogram(df, x='Grade', color='Class', title='成績(jī)分布圖')
ng.plotly(fig)
在優(yōu)化后的示例代碼中,我們通過使用NiceGUI的標(biāo)題、部分標(biāo)題和按鈕樣式,以及Plotly庫(kù)創(chuàng)建的交互式圖表,使圖形化界面更加漂亮美觀現(xiàn)代時(shí)尚。用戶可以通過按鈕點(diǎn)擊顯示成績(jī)分布圖,以便更直觀地了解學(xué)生成績(jī)情況。
十二、面向?qū)ο蟾聝?yōu)化程序示例代碼
以下是面向?qū)ο缶幊痰氖纠a,優(yōu)化了圖形化界面,使其更加漂亮美觀現(xiàn)代時(shí)尚:
import nicegui as ng
import pandas as pd
import plotly.express as px
class StudentGradeAnalyzer:
def __init__(self):
self.students = []
self.grades = []
self.classes = []
def import_data(self, file):
if file.name.endswith('.csv'):
df = pd.read_csv(file)
elif file.name.endswith('.xlsx'):
df = pd.read_excel(file)
self.students = df['Student'].tolist()
self.grades = df['Grade'].tolist()
self.classes = df['Class'].tolist()
def show_data(self):
ng.text('學(xué)生成績(jī)數(shù)據(jù):')
for student, grade, class_ in zip(self.students, self.grades, self.classes):
ng.text(f'{student}: {grade} - {class_}')
def export_excel(self):
df = pd.DataFrame({'Student': self.students, 'Grade': self.grades, 'Class': self.classes})
ng.file_download(df.to_excel(), filename='grades.xlsx', label='導(dǎo)出為Excel')
def export_csv(self):
df = pd.DataFrame({'Student': self.students, 'Grade': self.grades, 'Class': self.classes})
ng.file_download(df.to_csv(), filename='grades.csv', label='導(dǎo)出為CSV')
def filter_data(self, filter_value):
filtered_students = [student for student, grade in zip(self.students, self.grades) if grade >= filter_value]
ng.text('過濾后的學(xué)生成績(jī)數(shù)據(jù):')
for student in filtered_students:
ng.text(student)
def compare_data(self, compare_option, selected_option):
if compare_option == '班級(jí)':
class_students = [student for student, class_ in zip(self.students, self.classes) if class_ == selected_option]
ng.text(f'班級(jí)為{selected_option}的學(xué)生成績(jī)數(shù)據(jù):')
for student in class_students:
ng.text(student)
elif compare_option == '學(xué)科':
ng.text('學(xué)科比較功能暫未實(shí)現(xiàn)')
def visualize_data(self):
df = pd.DataFrame({'Student': self.students, 'Grade': self.grades, 'Class': self.classes})
fig = px.histogram(df, x='Grade', color='Class', title='成績(jī)分布圖')
ng.plotly(fig)
# 創(chuàng)建學(xué)生成績(jī)分析器實(shí)例
analyzer = StudentGradeAnalyzer()
# 創(chuàng)建界面
with ng.box():
ng.title('學(xué)生成績(jī)分析')
if ng.button('導(dǎo)入數(shù)據(jù)').clicked:
uploaded_file = ng.file_upload()
if uploaded_file:
analyzer.import_data(uploaded_file)
ng.text('數(shù)據(jù)導(dǎo)入成功!')
if ng.button('展示數(shù)據(jù)').clicked:
analyzer.show_data()
if ng.button('導(dǎo)出為Excel').clicked:
analyzer.export_excel()
if ng.button('導(dǎo)出為CSV').clicked:
analyzer.export_csv()
ng.section('數(shù)據(jù)過濾')
filter_value = ng.input('輸入過濾條件(成績(jī)大于等于多少):', type=float)
analyzer.filter_data(filter_value)
ng.section('數(shù)據(jù)比較')
compare_option = ng.select('選擇比較對(duì)象', ['班級(jí)', '學(xué)科'])
if compare_option == '班級(jí)':
class_options = list(set(analyzer.classes))
selected_class = ng.select('選擇班級(jí)', class_options)
analyzer.compare_data(compare_option, selected_class)
elif compare_option == '學(xué)科':
analyzer.compare_data(compare_option, None)
ng.section('數(shù)據(jù)可視化')
if ng.button('顯示成績(jī)分布圖').clicked:
analyzer.visualize_data()
在優(yōu)化后的示例代碼中,我們將學(xué)生成績(jī)分析功能封裝到了一個(gè)名為StudentGradeAnalyzer
的類中,通過面向?qū)ο缶幊痰姆绞綄?shí)現(xiàn)了界面優(yōu)化。通過創(chuàng)建一個(gè)學(xué)生成績(jī)分析器實(shí)例,并調(diào)用其方法來(lái)處理數(shù)據(jù)導(dǎo)入、展示、導(dǎo)出、過濾、比較和可視化等功能,使界面更加漂亮美觀現(xiàn)代時(shí)尚。
十三、歸納總結(jié)
在這個(gè)面向?qū)ο缶幊痰氖纠a中,我們可以總結(jié)出以下知識(shí)點(diǎn):
-
面向?qū)ο缶幊蹋∣OP):使用類和對(duì)象來(lái)組織代碼,將數(shù)據(jù)和操作封裝在一起,實(shí)現(xiàn)代碼的模塊化和重用。
-
類和對(duì)象:通過定義類
StudentGradeAnalyzer
來(lái)創(chuàng)建學(xué)生成績(jī)分析器實(shí)例analyzer
,實(shí)現(xiàn)數(shù)據(jù)處理和操作。 -
實(shí)例方法:在類中定義的方法(函數(shù)),用于操作實(shí)例的數(shù)據(jù)和狀態(tài)。
-
數(shù)據(jù)處理:導(dǎo)入數(shù)據(jù)、展示數(shù)據(jù)、導(dǎo)出數(shù)據(jù)、過濾數(shù)據(jù)、比較數(shù)據(jù)和可視化數(shù)據(jù)等功能。
-
界面優(yōu)化:使用
nicegui
庫(kù)創(chuàng)建美觀的圖形化界面,包括按鈕、文本框、下拉框、文件上傳、文件下載等交互元素。 -
數(shù)據(jù)分析和可視化:通過
pandas
庫(kù)處理數(shù)據(jù),使用plotly
庫(kù)創(chuàng)建直方圖可視化成績(jī)分布。 -
事件處理:通過按鈕點(diǎn)擊事件等交互操作,觸發(fā)相應(yīng)的數(shù)據(jù)處理和展示操作。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-830076.html
以上是這個(gè)示例代碼涉及的主要知識(shí)點(diǎn),通過這個(gè)示例可以了解如何利用面向?qū)ο缶幊毯蛨D形化界面優(yōu)化來(lái)實(shí)現(xiàn)學(xué)生成績(jī)分析功能。希望這些總結(jié)對(duì)您有幫助!如果您想進(jìn)一步了解某個(gè)知識(shí)點(diǎn),也可以隨時(shí)詢問我。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830076.html
到了這里,關(guān)于第七篇【傳奇開心果系列】Python微項(xiàng)目技術(shù)點(diǎn)案例示例:數(shù)據(jù)可視化界面圖形化經(jīng)典案例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!