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 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 test_nonmatching_url_is_not_replaced(self): for url in INVALID_URLS: assert url == url_util.process_gitblob_url(url)
def test_gist_url_is_replaced(self): for (target, processed) in GIST_URLS: assert url_util.process_gitblob_url(target) == processed