Movatterモバイル変換


[0]ホーム

URL:


Open In App

In this article, we will learn about NiceGUI, It is a Python-based UI Framework, which shows up in Web Browsers and we can create buttons, dialogs, Markdown, 3D scenes, plots, and much more. It works well for dashboards, robotics initiatives, smart house solutions, and other related use cases. Additionally, it can be applied in the creation process, such as when modifying or setting a machine learning method or adjusting motor controllers.

Users only need to take care of thePython code and it will handle the web development i.e GUI details. It is more lightweight than Streamlit and is not only available as a PyPI package but also in Docker. One of the best things about this is that whenever we modify our Python code it will automatically refresh and the changes will be reflected in our browser, no need to manually reload the browser or run the code every time.

Features of NiceGUI

  • Browser-based interaction with a graphical user
  • Standard GUI components like label, icon, checkbox, switch, scale, input, file upload, etc., implicitly refresh when the code is changed.
  • A straightforward organization using rows, columns, cards, and dialogue boxes
  • General-purpose Markdown and HTML components
  • Powerful high-level components to create 3D landscapes, draw graphs and charts, and receive steering inputs from simulated joysticks
  • Engage with tables, comment, and overlay pictures, and traverse foldable tree structures
  • Built-in schedule to periodically update the info (even every 10 ms)
  • Simple data coupling to create even less code
  • Modern user contact is provided by notifications, dialogs, and menus on both public and private online sites.
  • Adding custom paths and data replies
  • Recording keystrokes for use in universal shortcuts, etc.
  • Define main, secondary, and accent colors to create a unique appearance

Required module

To install NiceGUI write the below command in any terminal.

python3 -m pip install nicegui

Simple GUI Program using NiceGUI 

Now we will display a simple hello message using NiceGUI. These messages are known aslabels and the method we will use to add any text is also called alabel.

Python3
# Importing the modulefromniceguiimportui# passing the text we will showui.label('Hello Geeks from NiceGUI!')# running itui.run()

To run this code simply save it and execute it normally like any other Python code. either using the run button or writing the below command in terminal.

python <filename>.py

This run opens up users' default browser in localhost and every time we make any change to our code just save it and the page will reload automatically, no need to explicitly reload it.

 

Output:

 

We can display some text also using theset_text() method of the label. Even if we have something written inside a normal label, only the text passed inset_text will be shown, and the other one will be replaced.

Python3
fromniceguiimportui# set_text will replace the text# passed in labelui.label('Hello Geeks from NiceGUI!').set_text("This text is passed in set_text")ui.run()

Output:

 

Setting visibility of the text

We can explicitly set the visibility of the text using theset_visibility() method, which takes one parameter eitherTrue orFalse.

Python3
fromniceguiimportuiui.label().set_text(text="This text is passed in set_text")ui.label("Text visible or not").set_visibility(False)ui.run()

Output:

 

Adding Icons

Now we will see how we can add icons using NiceGUI using theicon() method.

Python3
fromniceguiimportuiui.label().set_text(text="This text is passed in set_text.")ui.icon("lock")ui.run()

Output:

 

Changing the color and the size of the Icon

Python3
fromniceguiimportuiui.label().set_text(text="This text is passed in set_text.")ui.icon("lock",color="green",size="50px")ui.run()

Output:

Introduction to NiceGUI
 

Users can use any icons available on the Google Fonts website and go to the Icon section. Just pass the name of the font as a parameter and add styling if necessary.

Set the Visibility of the Icon using set_visibility() Method

Visibility is set to False.

Python3
fromniceguiimportuiui.label().set_text(text="This text is passed in set_text.")# visibility is set to Falseui.icon("lock",color="green",size="50px").set_visibility(False)ui.run()

Output:

Introduction to NiceGUI
 

Visibility is set to True 

Python3
fromniceguiimportuiui.label().set_text(text="This text is passed in set_text.")# visibility is set to Trueui.icon("lock",color="green",size="50px").set_visibility(True)ui.run()

Output:

Introduction to NiceGUI
 

Adding UI Elements

In this example, we are trying to learn how we can insert a toggle, slider, and number container in our GUI web page using NiceGUI using Python.

Python3
fromniceguiimportuiclassDemo:def__init__(self):self.number=1demo=Demo()# creating sliderui.slider(min=1,max=5).bind_value(demo,'number')# creating numberui.number().bind_value(demo,'number')# creating toggle buttonsui.toggle({1:'A',2:'B',3:'C',4:'D',5:'E'}).bind_value(demo,'number')ui.run()

Output:

Introduction to NiceGUI
 

Adding Avatars

We can also add Avatars of our choice, it can be any icon (available Material Icons website) or any downloaded image or URL of an image.

Python3
fromniceguiimportui# The name of these icons is available in Google Fonts or# Material Icons website. Only those names will be recognizedui.avatar('adb')ui.run()

Output:

By default, we can see that the avatar color is black and the background is blue and the structure is a circle. We can change all of them according to our needs.

Introduction to NiceGUI
 

Changing color, background color, size, structure, and shape of the Avatar

Python3
fromniceguiimportuiui.avatar('adb',text_color='green',square=True,color='yellow',size="50px")ui.run()

Output:

Introduction to NiceGUI
 

Adding Hyperlink

Now we will see how can we add hyperlinks using NiceGUI. For that, we will use thelink() method, which takes two parameters. First, the text will be shown and the second is the link to which the link will redirect after clicking it.

Python3
fromniceguiimportuiui.link("The best Computer Science Portal","https://www.geeksforgeeks.org/")ui.run()

Output:

Introduction to NiceGUI
 

Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp