Logo

dev-resources.site

for different kinds of informations.

Building a Streamlit Inventory Management App with Fragment Decorators 🚀

Published at
12/12/2024
Categories
streamlit
python
fragment
app
Author
blamsa0mine
Categories
4 categories in total
streamlit
open
python
open
fragment
open
app
open
Author
11 person written this
blamsa0mine
open
Building a Streamlit Inventory Management App with Fragment Decorators 🚀

Introduction

Streamlit provides developers with an easy and intuitive way to build interactive web apps. One of the advanced features of Streamlit is the @st.fragment decorator, which helps modularize apps and improve performance.

In this tutorial, we'll walk you through the creation of an inventory management app with the following functionalities:

  1. Add items to an inventory.
  2. Display the current inventory.
  3. Delete specific items from the inventory.
  4. Avoid adding duplicate items.

Why Use @st.fragment?

The @st.fragment decorator in Streamlit allows developers to encapsulate specific functionalities, leading to modular and reusable code. When used effectively, it helps in reducing unnecessary re-runs and ensures the app remains responsive.

Full Code Example

Below is the complete code for the inventory management app:

import streamlit as st

# Initialize session state if not already set
if "inventory" not in st.session_state:
    st.session_state.inventory = []

@st.fragment
def add_item_fragment():
    """
        Fragment to add a new item to the inventory.
    """
    st.subheader('Add an Item')
    with st.form(key='add_item_form'):
        item_name = st.text_input("Item Name", placeholder="Example: Chair")
        item_quantity = st.number_input('Quantity', min_value=1, step=1, value=1)
        submitted = st.form_submit_button("Add to Inventory")
        if submitted:
            if item_name.strip():
                # Check for duplicates
                if any(item['name'] == item_name.strip() for item in st.session_state.inventory):
                    st.error("This item already exists in the inventory.")
                else:
                    st.session_state.inventory.append({"name": item_name.strip(), "quantity": item_quantity})
                    st.success(f"Item added: {item_name} ({item_quantity})")
                    st.rerun()
            else:
                st.error("Item name cannot be empty.")

@st.fragment
def display_inventory_fragment():
    """
    Fragment to display the inventory items.
    """
    st.subheader('Current Inventory')
    if st.session_state.inventory:
        for idx, item in enumerate(st.session_state.inventory):
            st.write(f"{idx + 1}. **{item['name']}** : {item['quantity']}")

        if st.button("Clear Inventory"):
            st.session_state.inventory = []
            st.success('Inventory cleared')
            st.rerun()
    else:
        st.info('Your inventory is empty.')

@st.fragment
def delete_item_fragment():
    """
    Fragment to delete an item from the inventory.
    """
    st.subheader('Delete an Item')
    if st.session_state.inventory:
        item_to_delete = st.selectbox("Select an item to delete", [item['name'] for item in st.session_state.inventory])
        if st.button("Delete Item"):
            st.session_state.inventory = [item for item in st.session_state.inventory if item['name'] != item_to_delete]
            st.success(f"Item deleted: {item_to_delete}")
            st.rerun()
    else:
        st.warning('No items to delete.')

# Main interface
st.title("Inventory Management App")

# Use fragments
add_item_fragment()
display_inventory_fragment()
delete_item_fragment()
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

1. Session State Initialization

We use st.session_state to store the inventory, ensuring persistence across user interactions. If the session state is empty, an empty inventory is initialized.

if "inventory" not in st.session_state:
    st.session_state.inventory = []
Enter fullscreen mode Exit fullscreen mode

2. Adding Items to Inventory

The add_item_fragment provides a form for users to input the name and quantity of an item. It includes validation to prevent adding items with empty names or duplicate entries. A success or error message is displayed based on the user's input.

if any(item['name'] == item_name.strip() for item in st.session_state.inventory):
    st.error("This item already exists in the inventory.")
else:
    st.session_state.inventory.append({"name": item_name.strip(), "quantity": item_quantity})
    st.success(f"Item added: {item_name} ({item_quantity})")
    st.rerun()
Enter fullscreen mode Exit fullscreen mode

3. Displaying the Inventory

The display_inventory_fragment iterates over the inventory and lists all items with their quantities. A button is provided to clear the entire inventory.

if st.session_state.inventory:
    for idx, item in enumerate(st.session_state.inventory):
        st.write(f"{idx + 1}. **{item['name']}** : {item['quantity']}")
    if st.button("Clear Inventory"):
        st.session_state.inventory = []
        st.success('Inventory cleared')
        st.rerun()
Enter fullscreen mode Exit fullscreen mode

4. Deleting Items from Inventory

The delete_item_fragment allows users to select an item by its name and delete it. After deletion, the inventory is updated and the app refreshes to reflect the changes.

if st.button("Delete Item"):
    st.session_state.inventory = [item for item in st.session_state.inventory if item['name'] != item_to_delete]
    st.success(f"Item deleted: {item_to_delete}")
    st.rerun()
Enter fullscreen mode Exit fullscreen mode

Key Features of This App

  • Avoid Duplicate Items: Items with the same name are not allowed.
  • Real-Time Updates: The app refreshes immediately after adding, deleting, or clearing items.
  • Modularity: Using @st.fragment ensures clean and reusable code structure.

Potential Improvements

  1. Export and Import Functionality:
    • Add options to export the inventory to a CSV or JSON file and re-import it.
  2. Search Capability:
    • Implement a search bar to filter items in the inventory.
  3. Categories and Units:
    • Allow users to categorize items or specify units (e.g., kilograms, pieces).

Conclusion

This inventory management app demonstrates the power of Streamlit's @st.fragment decorator for modular and efficient app development. By handling duplicate entries and providing real-time feedback, it offers a seamless user experience.

Feel free to customize this app further to suit your needs, and let us know in the comments how you plan to use Streamlit in your projects! 🚀


Happy coding! 🎉

app Article's
30 articles in total
Favicon
Top 7 Countries to Hire App Developers Offshore in 2025
Favicon
Building a Custom Star Rating Component in React Native with Sliding and Press Interactions
Favicon
The Importance of Security in PacketSDK for App Developers to Make Money
Favicon
Why API Development is Crucial for Modern Applications
Favicon
Emerging Trends in iOS App Development: Innovations Shaping the Future
Favicon
How to market your idea intro reality.
Favicon
Sustainability Metrics for Application Maintenance: A New Frontier in IT Practices
Favicon
PacketSDK: Understanding the Importance of SDKs in Modern Application Development
Favicon
Best Conga Alternative: Salesforce Document Generation Tool for Streamlined Workflows
Favicon
How to Choose a Mobile App Development Company
Favicon
5 Ways AI Is Reshaping Language Learning Apps
Favicon
Mastering PacketSDK: The New Art of App Monetization
Favicon
PacketSDK: Exploring Unique App Monetization Strategies for Success
Favicon
10 Must Have Features for Your Taxi Booking App Like Uber
Favicon
Expert-Approved Gmail Backup
Favicon
Introduction to Enterprise Mobile App Development
Favicon
Fitness App Development Company in India: Empowering Health and Wellness
Favicon
Just Knock Us > Telegram: @Smmpvashop
Favicon
Tailored Sonic Blockchain App Development Services for Your Business Needs
Favicon
Seamless Mail Backup Solution for Consistent and Long-Term Email Safety
Favicon
XM Trading App Download Latest Version The XM app is an online financial platform for Android. It is also user-friendly and simple to use for everyone. It permits you to trade forex, commodities, stocks, etc. from your phone or tablet. https://apktops.net
Favicon
Maximize Your Shopify App Performance: Essential Tips for E-commerce Success
Favicon
Step-by-Step Guide to Building a Successful eCommerce App
Favicon
TikTok Vidoe download tool?
Favicon
How developers can increase revenue through SDK
Favicon
How can App Developers Earn an iPhone16 In the Shortest Time
Favicon
Building a Streamlit Inventory Management App with Fragment Decorators 🚀
Favicon
PacketSDK: Guide You On How to Choose the Right Monetization Strategy for Your Application
Favicon
Starting from Scratch: How Novice Developers Can Make Income Quickly
Favicon
How to test iOS App without iPhone? [Why and How-to Guide]

Featured ones: