How to stop reloading the page after selecting any widget with Python in Streamlit

Currently, I have 2 issues as described below:

I> The page is always reloaded when I select any widget with Streamlit.

How to reproduce:

  1. Load page
  2. Select folder
  3. Select any item in the multi-selection
  4. Current: Page is reloaded

Expectation : Stop reloading the page.

II> The button Import source runs automatically

How to reproduce:

  1. Load page
  2. Select folder
  3. Observe the label
  4. Current: the text The button runs? is displayed

Expectation : The text is not displayed, and the function only runs when clicking on the button.

Main.py

import streamlit as st
import Views.DBImport as dbImport
import Controllers.MongoDBConnection as con
import Processor as processor
import Scraper as scraper
import os
import tkinter as tk
from tkinter import filedialog
import numpy as np

st.title('Test System')

def ProcessFile(dirName):
    fileList = []
    dirlist = []
    fileName = None
    for root, dirs, files in os.walk(dirName):
        for file in files:
            fileList.append([file])
            dirlist.append([dirPath + '/' + file])
            if not fileName:
                fileName = file.split(".")[0]
    if not fileList or not dirlist:
        st.write('Cannot read source folder!')
        st.stop()
    return np.concatenate((fileList, dirlist), axis=1), fileName

def ImportData(title, totalRecords, options, sampleRate, times):
    title = title + sampleRate
    return title

def ReadProperty(fileName, dirName):
    ecgProperty = processor.GetSourceProperty(fileName, dirName)
    # st.write(ecgProperty.channel)
    if not ecgProperty.channel:
        st.write('Cannot read source property!')
        st.stop()

    col1, col2, col3 = st.columns(3)
    with col1:
        title = st.text_input('Source name', 'Test')
        st.text('Total records')
        totalRecords = st.text(str(ecgProperty.record))
    with col2:
        options = st.multiselect(
            'Channel(s)',
            ['I', 'II', 'III',
             'aVR', 'aVL', 'aVF',
             'V1', 'V2', 'V3',
             'V4', 'V5', 'V6'])
        st.text('Total channels')
        st.text(str(len(options)))
    with col3:
        sampleRate = st.slider('Sample rate', 0, 10000,
                               ecgProperty.sample_rate)
        st.text('Time(s)')
        times = st.text(str(ecgProperty.time))

    if st.button('Import source'):
        result = ImportData(title, totalRecords, options, sampleRate, times)
        st.write('result: %s' % result)
    else:
        st.write('The button runs?')

# Set up tkinter
root = tk.Tk()
root.withdraw()

# Make folder picker dialog appear on top of other windows
root.wm_attributes('-topmost', 1)

# Folder picker button
st.text('Please select a folder:')
clicked = st.button('Folder Picker')
if clicked:
    dirPath = filedialog.askdirectory(master=root)
    dirName = st.text_input('Selected folder:', dirPath)
    # Process to get the list of files when selecting the folder
    fileList, fileName = ProcessFile(dirName)

    # Read ECG properties when user selects a source
    ReadProperty(fileName, dirName)
1 Like

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