How i can avoiding refresh the script if i click button


“I am facing an issue with my script and I’m unsure of how to resolve it. My application is designed to perform a simple task of scanning a barcode and matching it with the barcodes stored in my data. When a match is found, the application should display the minimum stock level. To achieve this, I have incorporated a button that triggers the barcode scanning function. Once the button is clicked, the application scans and checks for a match.”

2. the app show this interface

the probleme is if i click + or - or valid in seconde interface the script is re run again and

import pymongo
import streamlit as st 
from pymongo import MongoClient
import cv2
from pyzbar import pyzbar

# Connexion à la base de données MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")

# Sélection de la base de données et de la collection
db = client["database"]
PDR = db["PDR"]

st.set_page_config( layout="wide")
st.title('Magasine de les Pieces de Rechange')

if st.button('scanner') :
    # Capture d'une seule image de la caméra
    cap = cv2.VideoCapture(0)
    ret, frame =

    # Recherche des codes-barres dans l'image
    barcodes = pyzbar.decode(frame)

    # Boucle sur les codes-barres trouvés
    for barcode in barcodes:
        # Extraction des informations du code-barres
        barcode_data ="utf-8")
        barcode_type = barcode.type

        # Affichage des informations du code-barres
        print("Type de code-barres:", barcode_type)
        print("Données du code-barres:", barcode_data)

        # Libération des ressources


        DataCodeBar = PDR.find_one({'CodeBar': str(barcode_data)})
        if DataCodeBar:
            col1, col2, col3, col4, col5, col6, col7 = st.columns(7)
            col3.metric("MinStock", str(DataCodeBar['MinStock']))
            col5.metric("Stockinitial", str(DataCodeBar['Stockinitail']))
            number = col1.number_input('Insert a number', step = 1)
            if col2.button('Valide'):
                if DataCodeBar is not None:
                    new_stock = int(DataCodeBar['stockactuel']) - number
                    col4.metric("StockActuel", str(new_stock), delta=(-1)*number)
                    PDR.update_one(DataCodeBar, {"$set" : {"stock-actuel" : new_stock}})
            col6.metric("code KEN", str(DataCodeBar['CodeBar']))
            col7.metric("place", str(DataCodeBar['place']))
            st.write('Ce produit n\'existe pas dans la base de données.')


you can store status of that button in session states.
This might help you: Session state and button

1 Like

i trying session states but its not working i think because i am beginner in streamlit
if you want are you can give me the script thank you