,

Creating HTML GUIs in Python using NiceGUI

Posted by

To create HTML GUIs in Python with NiceGUI, you can follow these steps:

Step 1: Install NiceGUI
First, you need to install the NiceGUI library. You can do this using pip by running the following command:

pip install nicegui

Step 2: Import the necessary modules
Once NiceGUI is installed, you can import the necessary modules in your Python script. Here’s an example of how to import the required modules:

from nicegui import ui

Step 3: Create the HTML GUI
Now, you can start creating your HTML GUI using NiceGUI. You can add elements such as buttons, input fields, and labels to your GUI. Here’s an example of how to create a simple GUI with a button and a label:

with ui.page('My HTML GUI'):
    button_clicked = ui.button('Click me!')
    label = ui.markdown(value='Button not clicked yet')

    @button_clicked.on('click')
    def on_button_click(e):
        label.value = 'Button clicked!'

In this example, we create a page with the title ‘My HTML GUI’. We add a button with the label ‘Click me!’ and a label with the initial value ‘Button not clicked yet’. We also attach an event handler to the button that changes the label value to ‘Button clicked!’ when the button is clicked.

Step 4: Run the GUI
Finally, you can run your HTML GUI by calling the ui.run() function. This will open a browser window displaying your GUI. Here’s an example of how to run the GUI:

ui.run()

That’s it! You have now created a simple HTML GUI in Python using NiceGUI. You can customize your GUI further by adding more elements and styling it with CSS. NiceGUI also provides more advanced features, such as layouts and dialogs, that you can explore to create more complex GUIs.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@bennguyen1313
2 months ago

So is NiceGui an alternative to Flask?

What kind of back-end (server) support is needed?

I'd like to make a browser-based application that can access hardware peripherals, do you think that's doable or should I try something else? Flutter + WebUSB?

@viddeshk8020
2 months ago

Vodka 😉😏

Can we host the webapp of nice gui like we used to do in streamlit?