Text input, scan area (st.text_input)

i am currently running the app locally,

Python – 3.12
Streamlit – 1.33

I am using streamlit as my frontend and fastapi as my backend, In this code i am facing an problem, where i am selecting few dropdown and getting data from api. and in text scan area, when ever i scan first time cursor is moving from the scan area. and if again click and scan the rest of details the cursor doesn’t move the scan area.

i am unable find, where is it is going wrong.

import streamlit as st
import requests
from PIL import Image
from io import BytesIO
import pandas as pd
import base64
from streamlit import session_state as ss
from io import BytesIO

if "style_data" not in ss:
    ss.style_data = []

if "input_style" not in ss:
    ss.input_style = ''

if "src" not in ss:
    ss.src = ''

if "dst" not in ss:
    ss.dst = ''

if "grt" not in ss:
    ss.grt = ''

if "input_dd" not in ss:
    ss.input_dd = ''

# Streamlit configuration

API_URL = "http://localhost:8000"

def fectch_dest_sites():
        src_response =  requests.get(f"{API_URL}/receiver_sites/")
        if src_response.status_code == 200:
            return src_response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error: {e}")
        return None

def fectch_src_sites(receiver):
        src_response =  requests.get(f"{API_URL}/receiver_for_senders/{receiver}")
        if src_response.status_code == 200:
            return src_response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error: {e}")
        return None

def fectch_grt_docs(receiver,sender):
        grt_response =  requests.get(f"{API_URL}/grt_no_for_sender_receiver/?receiver_code={receiver}&sender_code={sender}")
        if grt_response.status_code == 200:
            return grt_response.json()
    except requests.exceptions.RequestException as e:
        st.error(f"Error: {e}")
        return None

def fetch_item_info(receiver,sender, grt, input_dd):
        if input_dd:
            # If ICODE not found, try BARCODE
            item_response = requests.get(f"{API_URL}/info/?receiver_code={receiver}&sender_code={sender}&grt={grt}&barcode={input_dd}")

            # Check if input_dd is an ICODE
            if item_response.status_code != 200:
                item_response = requests.get(f"{API_URL}/info/?receiver_code={receiver}&sender_code={sender}&grt={grt}&icode={input_dd}")

            if item_response.status_code == 200:
                return item_response.json()
            st.error("Please provide either 'ICODE' or 'BARCODE'")
    except requests.exceptions.RequestException as e:
        st.error(f"Error: {e}")
        return None
def get_image_from_url(url):
        response = requests.get(url)
        img = Image.open(BytesIO(response.content))
        return img
    except Exception as e:
        st.error(f"Error fetching image from URL: {e}")
        return None

def get_thumbnail(url):
    img = get_image_from_url(url)
    if img:
        img.thumbnail((200, 200))
    return img

def image_to_base64(img):
    if img:
        with BytesIO() as buffer:
            return base64.b64encode(buffer.getvalue()).decode()

def image_formatter(url):
    img = get_thumbnail(url)
    if img:
        return f'<img src="data:image/png;base64,{image_to_base64(img)}">'

def create_anchor_tag(url):
        return f'<a href="{url}" target="_blank">{url}</a>'

def convert_df(input_df):
    # Formatting functions
    input_df['Image'] = input_df['Image Link'].apply(image_formatter)
    input_df['Image Link'] = input_df['Image Link'].apply(create_anchor_tag)
    return input_df.to_html(escape=False, formatters={'Image Link': lambda x: x, 'Image': lambda x: x}, index=False)

def new_styles_cb():
    """Append data to style_data from selection."""
            "Receiver" : ss.receiver,
            "Receiver Code" : ss.receiver_code,
            "Sender ": ss.sender,
            "Sender Code ": ss.sender_code,
            "GRT_No" : ss.grt_no,
            "GRT_Date" : ss.grt_dt,
            "Department" : ss.department,
            "Icode" : ss.icode,
            "Barcode" : ss.barcode,
            "Style_No": ss.styleno,
            "Qty" : ss.qty,
            "RSP" : ss.rsp,
            "Image Link": ss.imagelink

def fetch_data_new():
    style_info = fetch_item_info(ss.dst,ss.src,ss.grt,ss.input_dd)

    # Display style information in a table
    if style_info:
        list_data = list(style_info.values())

        ss.sender = list_data[0]
        ss.sender_code = list_data[1]
        ss.receiver = list_data[2]
        ss.receiver_code = list_data[3]
        ss.grt_no = list_data[4]
        ss.grt_dt = list_data[5]
        ss.department = list_data[6]
        ss.icode = list_data[7]
        ss.barcode = list_data[8]
        ss.styleno = list_data[9]
        ss.qty = list_data[10]
        ss.rsp = list_data[11]
        ss.imagelink = list_data[12]
        ss.input_dd = ''

# Streamlit UI
st.title("Style Information")

a,b,c,d = st.columns(4)

dst_sites = fectch_dest_sites()
ds_d = list(dst_sites)

with a:
    st.selectbox("Destination Site:", ds_d, key="dst")

src_sites = fectch_src_sites(ss.dst)
if ss.dst != '':
    ss_d = list(src_sites)

if ss.src != '':
    with b:
        st.selectbox("Source Site:", ss_d, key="src")
else :
    with b:
        st.selectbox("Source Site:", '', key="src")

grt_docs = fectch_grt_docs(ss.dst,ss.src)
if  ss.dst != '' and ss.src != '':
    grt_d = list(grt_docs)

if  ss.dst != '' and ss.src != '':
    with c:
        st.selectbox("GRT Doc No:", grt_d, key="grt")
    with c:
        st.selectbox("GRT Doc No:", '', key="grt")

if ss.dst != '' and ss.src != '' and ss.grt != '' :
    with d:
        st.text_input("Enter Icode or Barcode", key='input_dd', on_change=fetch_data_new)
    with d:
        st.text_input("Enter Icode or Barcode", key='input_dd')

if (len(ss.style_data)) > 0:

    df_data = pd.DataFrame(ss.style_data)

    target_column  = 'Image Link'
    target_index = df_data.columns.get_loc(target_column)
    df_data.insert(target_index, 'Image', df_data['Image Link'])

    df_data_dw = df_data.drop(columns=['Image','Image Link'])

    csv_data = BytesIO()
    df_data_dw.to_csv(csv_data, index=False)
    # Convert the CSV data in BytesIO to Base64
    b64 = base64.b64encode(csv_data.read()).decode()
    # Create a download link to download the CSV file
    download_link = f'<a href="data:text/csv;base64,{b64}" download="style_data.csv">Download File</a>'
    st.markdown(download_link, unsafe_allow_html=True)


    html_view = convert_df(df_data)
    styled_html = f'''<style>
                      table {{ font-size: 12px; text-align: center; }}
                      th{{text-align: center !important;}}
    st.info('No Data')


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.