Dynamic Layout Creation - Help debug

All,
I am trying to create a dynamic layout, but I keep running into a bug and I am unable to debug it. Any help would be appreciated:

import streamlit as st
import pandas as pd


def default_ui():
    st.title("User Inputs")
    user_n_files = st.sidebar.number_input(
        label='# of files to read',
        min_value=2
    )
    load_flag = st.sidebar.checkbox(
        label='Load Browse Options',
        value=False
    )

    return user_n_files, load_flag


def add_file_loading_options(n_files: int):
    """ Add UI options for loading files

    :param n_files: # of files user wants to load
    """
    paths, field_1, field_2 = ([None] * n_files for k in range(3))

    columns = list(st.beta_columns(n_files))
    iteration_list = list(range(0, n_files))

    for i in iteration_list:
        with columns[i]:
            st.header = f"""Select file {i + 1}"""
            paths[i] = st.file_uploader(
                label=f"""Browse to file {i + 1}""",
                type='csv'
            )
            field_1[i] = int(
                st.slider(
                    label=f'''Enter column number for field_1 in file {i + 1}''',
                    step=1,
                    format='%d'
                )
            )
            field_2[i] = int(
                st.slider(
                    label=f'''Enter column number for field_2 in file {i + 1}''',
                    step=1,
                    format='%d'
                )
            )

            return paths, field_1, field_2


def main():
    """Central wrapper for controlling UI"""

    st.header('Streamlit Testing')

    n_files_to_read, flag = default_ui()

    if flag:
        paths, field_1, field_2 = add_file_loading_options(n_files_to_read)
        option_flag = st.button(label='Run Tool')

        if option_flag:
            all_data = [
                pd.read_csv(path) for path in paths
            ]

            for df in all_data:
                st.dataframe(df)


main()

image

Several things donโ€™t work as expected. When ever a file is browsed and selected, I get the above error. It doesnโ€™t even wait for the button to be clicked ? Any help is appreciated.

Thanks
Uday

Bump

I would highly advise you to please post the traceback along with the error. The traceback tells you where the error happened :slight_smile:

TypeError: 'str' object is not callable
Traceback:

File "/home/jupyter-pomkos/.conda/envs/mess_env/lib/python3.6/site-packages/streamlit/script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)
File "/home/jupyter-pomkos/test.py", line 74, in <module>
    main()
File "/home/jupyter-pomkos/test.py", line 57, in main
    st.header('Streamlit Testing')

I went ahead and tried your code on my end. The error says that โ€œstr object is not callableโ€, this means that somewhere you have a string that you thought was a function. The traceback states that this happens on line 57.

So hereโ€™s what happened: on line 31 st.header was assigned a string, this overrode the streamlit import of the header function. st.header is now f"Select file {i+1}" and not the header function. If you just replace st.header = f"Select file {i+1}" with st.header(f"Select file {i+1}") the file should upload.

The button works as expected from what I can tell. The csv will not be read into a dataframe until the button is clicked.

2 Likes

@pomkos,
You were absolutely right. The error was occurring because I had overwritten st.header(). Thanks a lot. Your advice helped fix my problem

Uday