def _apply_config_options_from_cli(kwargs): """The "streamlit run" command supports passing Streamlit's config options as flags. This function reads through all config flags, massage them, and pass them to _set_config() overriding default values and values set via config.toml file """ # Parse config files first before setting CLI args. # Prevents CLI args from being overwritten if not _config._config_file_has_been_parsed: _config.parse_config_file() for config_option in kwargs: if kwargs[config_option] is not None: config_option_def_key = config_option.replace("_", ".") _config._set_option( config_option_def_key, kwargs[config_option], "command-line argument or environment variable", ) _config._on_config_parsed.send()
def test_load_local_config(self): """Test that $CWD/.streamlit/config.toml is read, even if ~/.streamlit/config.toml is missing. """ local_config = """ [s3] bucket = "local_bucket" accessKeyId = "local_accessKeyId" """ local_config_path = os.path.join(os.getcwd(), ".streamlit/config.toml") open_patch = patch("streamlit.config.open", mock_open(read_data=local_config)) # patch streamlit.*.os.* instead of os.* for py35 compat makedirs_patch = patch("streamlit.config.os.makedirs") makedirs_patch.return_value = True pathexists_patch = patch("streamlit.config.os.path.exists") pathexists_patch.side_effect = lambda path: path == local_config_path with open_patch, makedirs_patch, pathexists_patch: config.parse_config_file() self.assertEqual("local_bucket", config.get_option("s3.bucket")) self.assertEqual("local_accessKeyId", config.get_option("s3.accessKeyId")) self.assertIsNone(config.get_option("s3.url"))
def test_missing_config(self): """Test that we can initialize our config even if the file is missing.""" with patch("streamlit.config.os.path.exists") as path_exists: path_exists.return_value = False config.parse_config_file() self.assertEqual(True, config.get_option("client.caching")) self.assertIsNone(config.get_option("s3.bucket"))
def test_load_global_local_config(self): """Test that $CWD/.streamlit/config.toml gets overlaid on ~/.streamlit/config.toml at parse time. """ global_config = """ [s3] bucket = "global_bucket" url = "global_url" """ local_config = """ [s3] bucket = "local_bucket" accessKeyId = "local_accessKeyId" """ global_config_path = "/mock/home/folder/.streamlit/config.toml" local_config_path = os.path.join(os.getcwd(), ".streamlit/config.toml") global_open = mock_open(read_data=global_config) local_open = mock_open(read_data=local_config) open = mock_open() open.side_effect = [global_open.return_value, local_open.return_value] open_patch = patch("streamlit.config.open", open) # patch streamlit.*.os.* instead of os.* for py35 compat makedirs_patch = patch("streamlit.config.os.makedirs") makedirs_patch.return_value = True pathexists_patch = patch("streamlit.config.os.path.exists") pathexists_patch.side_effect = lambda path: path in [ global_config_path, local_config_path, ] with open_patch, makedirs_patch, pathexists_patch: config.parse_config_file() # s3.bucket set in both local and global self.assertEqual("local_bucket", config.get_option("s3.bucket")) # s3.url is set in global, and not in local self.assertEqual("global_url", config.get_option("s3.url")) # s3.accessKeyId is set in local and not in global self.assertEqual("local_accessKeyId", config.get_option("s3.accessKeyId"))
def test_load_global_config(self): """Test that ~/.streamlit/config.toml is read.""" global_config = """ [s3] bucket = "global_bucket" url = "global_url" """ global_config_path = ( global_config_path) = "/mock/home/folder/.streamlit/config.toml" open_patch = patch("streamlit.config.open", mock_open(read_data=global_config)) makedirs_patch = patch("streamlit.config.os.makedirs") makedirs_patch.return_value = True pathexists_patch = patch("streamlit.config.os.path.exists") pathexists_patch.side_effect = lambda path: path == global_config_path with open_patch, makedirs_patch, pathexists_patch: config.parse_config_file() self.assertEqual(u"global_bucket", config.get_option("s3.bucket")) self.assertEqual(u"global_url", config.get_option("s3.url")) self.assertIsNone(config.get_option("s3.accessKeyId"))
from mock import patch, mock_open # Do not import any Streamlit modules here! See below for details. os.environ["HOME"] = "/mock/home/folder" CONFIG_FILE_CONTENTS = """ [global] sharingMode = "off" unitTest = true [browser] gatherUsageStats = false """ with patch( "streamlit.config.open", mock_open(read_data=CONFIG_FILE_CONTENTS), create=True), patch("streamlit.config.os.path.exists") as path_exists: # It is important that no streamlit imports happen outside of this patch # context. Some Streamlit modules read config values at import time, which # will cause config.toml to be read. We need to ensure that the mock config # is read instead of the user's actual config. from streamlit import file_util from streamlit import config config_path = file_util.get_streamlit_file_path("config.toml") path_exists.side_effect = lambda path: path == config_path config.parse_config_file(force=True)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Pytest fixtures. Everything in this file applies to all tests. This basically makes the tests not READ from your local home directory but instead this mock config. """ import os from contextlib import contextmanager from mock import patch, mock_open from streamlit import config os.environ["HOME"] = "/mock/home/folder" CONFIG_FILE_CONTENTS = """ [global] sharingMode = "off" unitTest = true [browser] gatherUsageStats = false """ config.parse_config_file(CONFIG_FILE_CONTENTS)
tests not READ from your local home directory but instead this mock config. """ import os from mock import patch, mock_open from streamlit import config from streamlit import util os.environ["HOME"] = "/mock/home/folder" CONFIG_FILE_CONTENTS = """ [global] sharingMode = "off" unitTest = true [browser] gatherUsageStats = false """ config_path = util.get_streamlit_file_path("config.toml") with patch( "streamlit.config.open", mock_open(read_data=CONFIG_FILE_CONTENTS), create=True), patch("streamlit.config.os.path.exists") as path_exists: path_exists.side_effect = lambda path: path == config_path config.parse_config_file()