Questions about "bare mode"

Hi Streamlit community,

I would like to confirm what bare mode is, and the implications (if any) of running in said bare mode.

Background:
I have developed a package with modules that contain common functions that I use in a couple of Streamlit apps. Those apps are deployed on premise. Many of the functions in those modules contain streamlit commands such as st.write, st.text, st.warning, etc. I would like to use this package in a non-streamlit project. By this, I mean this project is not a web app, it’s a script without a UI. I can install the package without issues, and obviously Streamlit is a dependency. When I call any of the functions that contain streamlit commands (st.write, st.warning…), I get the following warning, multiple times:

“Thread ‘MainThread’: missing ScriptRunContext! This warning can be ignored when running in bare mode.”

Question 1: Does “bare mode” mean calling Streamlit not as instructed in the documentation, ie. “Streamlit run my_app.py” ?

  • If so, as the warning says, I can safely ignore it, right?
  • How to get rid of it? (Note that I have tried showWarningOnDirectExecution = false in my config file, I still get the warning).

Question 2: If this is not bare mode, what is bare mode?

  • And what are implications/consequences (if any) calling streamlit commands when streamlit is not initiated as described in the docs (Streamlit run my_app.py)?

Question 3: Pretending it’s ok to use Streamlit as described above, what would be a good way to incorporate logging depending whether the functions are used within a Streamlit app, or as a script ? In my use case, the goal of those st.write, st.warning… commands is mostly to communicate events to users while they use the web app. If the functions are used in a script, I would still be able to log events using something like the python logging module. Here’s one possible implementation, but it requires a lot of modifications to the current code, but curious to know if anyone has a better idea:

# My module with streamlit calls
import streamlit as st
import logging

def my_func(use_case):
    if use_case == 'script':
        logger = logging.getLogger(__name__)
    #....
    # do some stuff and report event depending on use case...
    #....
    msg = "Something happened"
    if use_case == 'script':
        logger.info(msg)
    else:
        st.info(msg)

Please reopen this.

Not an answer to your question, I also don’t know what ‘bare mode’ means, but I have found a way to get rid of the warnings. This is what worked for me.

import streamlit as st
import logging

streamlit_loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict if name.startswith(‘streamlit’)]
for logger in streamlit_loggers:
logger.setLevel(logging.ERROR)

The logging levels need to be set before importing any other modules with streamlit commands in it otherwise you’ll still get a warning. Also, this modifies the logging levels for all of streamlits logging handlers. There are a lot and I don’t know which ones are giving the warning so I modified them all.