自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用Python構(gòu)建簡單的UI?

開發(fā) 后端
借助Streamlit框架,使用用戶界面展示Python項目變得前所未有的簡單,你可以僅僅使用Python代碼來構(gòu)建基于瀏覽器的UI。本次演示將為迷宮求解器程序構(gòu)建UI。

本文轉(zhuǎn)載自公眾號“讀芯術”(ID:AI_Discovery)。

借助Streamlit框架,使用用戶界面展示Python項目變得前所未有的簡單,你可以僅僅使用Python代碼來構(gòu)建基于瀏覽器的UI。

本次演示將為迷宮求解器程序構(gòu)建UI。

[[335352]]

Streamlit

Streamlit是一種Web框架,旨在供數(shù)據(jù)科學家使用Python輕松部署模型和可視化。它運行速度既快又簡約,代碼既漂亮又對用戶友好。

它們是有用于用戶輸入的內(nèi)置小部件,例如圖像上載、滑塊、文本輸入,以及其他熟悉的HTML元素(例如復選框和單選按鈕)。每當用戶與簡化的應用程序進行交互時,python腳本就會從頭到尾重新運行,這是在考慮應用程序的不同狀態(tài)時要記住的重要概念。

使用pip安裝Streamlit:

  1. Pip install streamlit 

并在python腳本上運行streamlit:

  1. Streamlit run app.py 

使用例子

我在上一篇文章中演示構(gòu)建了一個Python程序

(https://towardsdatascience.com/solving-mazes-with-python-f7a412f2493f),該程序?qū)⒔鉀Q給定圖像文件和起始/結(jié)束位置的迷宮。

現(xiàn)在,我希望將此程序變成一個單頁Web應用程序,用戶可以在其中上傳迷宮圖像(或使用默認迷宮圖像),調(diào)整迷宮的開始和結(jié)束位置,并查看最終解決的迷宮。

首先,為圖像上傳器創(chuàng)建UI,并選擇使用默認圖像的選項??梢允褂胹t.write()或st.title()之類的函數(shù)添加文本輸出,使用streamlit的st.file_uploader()函數(shù)存儲動態(tài)上傳的文件。最后,st.checkbox()將根據(jù)用戶是否已選中復選框返回一個布爾值。

  1. import streamlit as st 
  2.          import cv2 
  3.          import matplotlib.pyplot as plt 
  4.          import numpy as np 
  5.          import maze 
  6.                    st.title( Maze Solver ) 
  7.          uploaded_file = st.file_uploader("Choose an image", ["jpg","jpeg","png"]) #image uploader 
  8.          st.write( Or ) 
  9.          use_default_image = st.checkbox( Use default maze ) 

結(jié)果:

如何使用Python構(gòu)建簡單的UI?

然后,可以將默認圖像或上傳的圖像讀取為可用的OpenCV圖像格式。

  1. if use_default_image: 
  2.                        opencv_image = cv2.imread( maze5.jpg ) 
  3.                                      elif uploaded_file isnotNone: 
  4.                        file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) 
  5.                        opencv_image = cv2.imdecode(file_bytes, 1) 

上載圖像后,要顯示標記有起點和終點的圖像。將使用滑塊允許用戶重新定位這些點。st.sidebar()函數(shù)在頁面上添加了一個側(cè)邊欄,st.slider()在定義的最小值和最大值內(nèi)接受數(shù)字輸入,你可以根據(jù)迷宮圖像的大小動態(tài)定義滑塊的最小值和最大值。

  1. if opencv_image isnotNone: 
  2.                 st.subheader( Use the sliders on the left to position the start and endpoints ) 
  3.                 ststart_x = st.sidebar.slider("Start X", value=24if use_default_image  else50, min_value=0max_value=opencv_image.shape[1], keysx ) 
  4.                 ststart_y = st.sidebar.slider("Start Y", value=332if use_default_image  else100, min_value=0max_value=opencv_image.shape[0], keysy ) 
  5.                 finish_x = st.sidebar.slider("Finish X", value=309if use_default_image  else100, min_value=0max_value=opencv_image.shape[1], keyfx ) 
  6.                 finish_y = st.sidebar.slider("Finish Y", value=330if use_default_image  else100, min_value=0max_value=opencv_image.shape[0], keyfy ) 
  7.                 marked_image = opencv_image.copy() 
  8.                 circle_thickness=(marked_image.shape[0]+marked_image.shape[0])//2//100#circle thickness based on img size 
  9.                 cv2.circle(marked_image,(start_x, start_y), circle_thickness, (0,255,0),-1) 
  10.                 cv2.circle(marked_image,(finish_x, finish_y), circle_thickness, (255,0,0),-1) 
  11.                 st.image(marked_image,channels="RGB"width=800
如何使用Python構(gòu)建簡單的UI?

每當用戶調(diào)整滑塊時,圖像都會快速重新渲染,并且點會更改位置。

一旦用戶確定了開始位置和結(jié)束位置,就需要一個按鈕來解決迷宮并顯示解決方案。僅在其子進程運行時顯示st.spinner()元素,并且使用st.image()調(diào)用顯示圖像。

  1. if marked_image isnotNone: 
  2.                 if st.button( Solve Maze ): 
  3.                     with st.spinner( Solving your maze ): 
  4.                         path = maze.find_shortest_path(opencv_image,(start_x,start_y),(finish_x, finish_y)) 
  5.                     pathed_image = opencv_image.copy() 
  6.                     path_thickness = (pathed_image.shape[0]+pathed_image.shape[0])//200 
  7.                     maze.drawPath(pathed_image,path, path_thickness) 
  8.                     st.image(pathed_image,channels="RGB"width=800
如何使用Python構(gòu)建簡單的UI?

Streamlit按鈕和微調(diào)器

如何使用Python構(gòu)建簡單的UI?

顯示解決的迷宮

瞧瞧,不需要編寫任何傳統(tǒng)的前端代碼,我們用不到40行代碼為Python圖像處理應用程序創(chuàng)建了一個簡單的UI。

事實上, Streamlit除了能夠消化簡單的Python代碼之外,無論用戶與頁面進行交互還是更改了腳本,Streamlit都會從上至下智能地重新運行腳本的必要部分,這樣可以實現(xiàn)直接的數(shù)據(jù)流和快速開發(fā),它讓一切變得簡單!

 

責任編輯:趙寧寧 來源: 讀芯術
相關推薦

2023-04-06 15:09:00

PythonGUI開發(fā)

2013-06-13 10:29:39

CasperJS測試UI測試

2010-03-12 19:03:48

Python 拼寫檢查

2022-11-14 11:41:13

SVG開發(fā)組件

2011-09-16 14:21:47

Web API

2023-09-19 08:00:00

Python開發(fā)

2024-08-01 13:12:57

2024-09-29 16:36:16

2016-09-21 12:54:10

CAAS系統(tǒng)鏡像

2016-09-14 17:48:44

2024-03-08 12:17:39

網(wǎng)絡爬蟲Python開發(fā)

2023-08-07 07:48:47

2023-09-05 09:00:00

工具Python抄襲檢測系統(tǒng)

2018-07-27 16:18:30

PythonTwitter機器人

2023-08-08 08:00:00

架構(gòu)Kafka

2024-05-17 09:00:45

SwiftUIvisionOS

2021-10-29 16:18:14

Streamlit Python

2022-04-01 15:36:05

Python推薦系統(tǒng)數(shù)據(jù)

2020-06-04 17:38:49

PythonFastAPIWeb服務

2021-12-02 07:50:31

混合云專線機房
點贊
收藏

51CTO技術棧公眾號