def main_run(target, args=None, **kwargs): """Run a Python script, piping stderr to Streamlit. The script can be local or it can be an url. In the latter case, Streamlit will download the script to a temporary file and runs this file. """ from validators import url _apply_config_options_from_cli(kwargs) if url(target): from streamlit.temporary_directory import TemporaryDirectory with TemporaryDirectory() as temp_dir: from urllib.parse import urlparse from streamlit import url_util path = urlparse(target).path script_path = os.path.join(temp_dir, path.strip("/").rsplit("/", 1)[-1]) # if this is a GitHub/Gist blob url, convert to a raw URL first. target = url_util.process_gitblob_url(target) _download_remote(script_path, target) _main_run(script_path, args) else: if not os.path.exists(target): raise click.BadParameter("File does not exist: {}".format(target)) _main_run(target, args)
def main_run(target, args=None, **kwargs): """Run a Python script, piping stderr to Streamlit. The script can be local or it can be an url. In the latter case, Streamlit will download the script to a temporary file and runs this file. """ from validators import url _apply_config_options_from_cli(kwargs) _, extension = os.path.splitext(target) if extension[1:] not in ACCEPTED_FILE_EXTENSIONS: raise click.BadArgumentUsage( "Streamlit requires raw Python (.py) files, not %s.\nFor more information, please see https://docs.streamlit.io" % extension) if url(target): from streamlit.temporary_directory import TemporaryDirectory with TemporaryDirectory() as temp_dir: from urllib.parse import urlparse from streamlit import url_util path = urlparse(target).path script_path = os.path.join(temp_dir, path.strip("/").rsplit("/", 1)[-1]) # if this is a GitHub/Gist blob url, convert to a raw URL first. target = url_util.process_gitblob_url(target) _download_remote(script_path, target) _main_run(script_path, args) else: if not os.path.exists(target): raise click.BadParameter("File does not exist: {}".format(target)) _main_run(target, args)
def test_temp_directory(self, dir): """Test that the directory only exists inside the context.""" with TemporaryDirectory(dir=dir.path) as temp_fname: self.assertTrue(os.path.exists(temp_fname)) self.assertFalse(os.path.exists(temp_fname))