Fetching st.dataframe by key during testing

I am beginning to use Streamlit’s testing framework on my apps, but I am unable to fetch st.dataframes by their key, only by their index. I would prefer to use keys as some apps are quite expansive. I’ve created a minimal example:

app.py

import pandas as pd
import streamlit as st

data = pd.DataFrame(
    {
        'A' : [1,2,3],
        'B' : [4,5,6]
    }
)

st.dataframe(data, key = 'dataframe_1')

test_app.py

from streamlit.testing.v1 import AppTest

def test_app():

    app = AppTest.from_file('app.py')
    app.run()
    assert app.dataframe(key = 'dataframe_1')

I get the following error:

platform win32 -- Python 3.10.0, pytest-8.3.2, pluggy-1.5.0
rootdir: C:\Users\colburn.hassman\Desktop\_streamlit_bug
plugins: typeguard-4.3.0
collected 1 item

test_app.py F                                                                                                                                [100%]

==================================================================== FAILURES ===================================================================== ____________________________________________________________________ test_app _____________________________________________________________________

    def test_app():

        app = AppTest.from_file('app.py')
        app.run()
>       assert app.dataframe(key = 'dataframe_1')
E       TypeError: 'ElementList' object is not callable

test_app.py:9: TypeError
-------------------------------------------------------------- Captured stderr call --------------------------------------------------------------- 2024-09-02 12:58:15.307 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode.
============================================================= short test summary info ============================================================= FAILED test_app.py::test_app - TypeError: 'ElementList' object is not callable
================================================================ 1 failed in 3.83s ================================================================

Running python==3.10, streamlit==1.38.0

When I access the dataframe using indexing, the .key attribute is None

app.dataframe[0].key

Is there a way to fetch st.dataframes by their key for testing purposes?

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