St.session_state has no attribute "files". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization

Summary

I get message error that st.session_state has no attribute “files” in the last line of the code bellow.

Steps to reproduce

Code snippet:

import streamlit as st
import os
import random

state = st.session_state
generated_path = 'generated_predictions.txt'

ORI_RES = f'path_ori'
# CONTEXT_SENT_0_h_0_RES = f'path_1/{generated_path}'
CONTEXT_SENT_0_h_1_RES = f'/path2/{generated_path}'
CONTEXT_SENT_1_h_1_RES = f'/path3/{generated_path}'

source_path = 'path_source'

OPTIONS = ["label 1", "label 2"]

if "annotations" not in state:
    state.annotations = {}
    f_0 = open(source_path)
    source = f_0.read().split('\n')[:-1]
    f_1 = open(ORI_RES)
    ori_res = f_1.read().split('\n')[:-1]
    # f_2 = open(CONTEXT_SENT_0_h_0_RES)
    f_3 = open(CONTEXT_SENT_0_h_1_RES)
    sent_0_h_1 = f_3.read().split('\n')[:-1]
    f_4 = open(CONTEXT_SENT_1_h_1_RES)
    sent_1_h_1 = f_4.read().split('\n')[:-1]
    state.files = list()
    state.files = list(zip(source, ori_res, sent_0_h_1, sent_1_h_1))
    state.current_file = state.files[0]

Actual behavior:
st.session_state has no attribute “files”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

Debug info

  • Streamlit version: 1.15.2
  • Python version: 3.9.15
  • Using Conda

Hi @rubenwol :wave:

Welcome to the Streamlit forum!

Could you please share a minimal reproducible example? The code you’ve shared is not reproducible. It leads to multiple errors unrelated to session state:

I was uable to reproduce the behavior with the following minimal snippet:

import streamlit as st

state = st.session_state

if "annotations" not in state:
    state.annotations = {}
    state.files = list()
    state.files.append("file1")
    state.current_file = state.files[0]