-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cli.py
284 lines (207 loc) · 8.05 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Copyright 2018-2021 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""This is a script which is run when the Streamlit package is executed."""
from streamlit import config as _config
import os
from typing import Optional
import click
import streamlit
from streamlit.credentials import Credentials, check_credentials
import streamlit.bootstrap as bootstrap
from streamlit.case_converters import to_snake_case
ACCEPTED_FILE_EXTENSIONS = ("py", "py3")
LOG_LEVELS = ("error", "warning", "info", "debug")
def _convert_config_option_to_click_option(config_option):
"""Composes given config option options as options for click lib."""
option = "--{}".format(config_option.key)
param = config_option.key.replace(".", "_")
description = config_option.description
if config_option.deprecated:
description += "\n {} - {}".format(
config_option.deprecation_text, config_option.expiration_date
)
envvar = "STREAMLIT_{}".format(to_snake_case(param).upper())
return {
"param": param,
"description": description,
"type": config_option.type,
"option": option,
"envvar": envvar,
}
def configurator_options(func):
"""Decorator that adds config param keys to click dynamically."""
for _, value in reversed(_config._config_options_template.items()):
parsed_parameter = _convert_config_option_to_click_option(value)
config_option = click.option(
parsed_parameter["option"],
parsed_parameter["param"],
help=parsed_parameter["description"],
type=parsed_parameter["type"],
show_envvar=True,
envvar=parsed_parameter["envvar"],
)
func = config_option(func)
return func
# Fetch remote file at url_path to script_path
def _download_remote(script_path, url_path):
import requests
with open(script_path, "wb") as fp:
try:
resp = requests.get(url_path)
resp.raise_for_status()
fp.write(resp.content)
except requests.exceptions.RequestException as e:
raise click.BadParameter(("Unable to fetch {}.\n{}".format(url_path, e)))
@click.group(context_settings={"auto_envvar_prefix": "STREAMLIT"})
@click.option("--log_level", show_default=True, type=click.Choice(LOG_LEVELS))
@click.version_option(prog_name="Streamlit")
@click.pass_context
def main(ctx, log_level="info"):
"""Try out a demo with:
$ streamlit hello
Or use the line below to run your own script:
$ streamlit run your_script.py
"""
if log_level:
import streamlit.logger
streamlit.logger.set_log_level(log_level.upper())
@main.command("help")
@click.pass_context
def help(ctx):
"""Print this help message."""
# Pretend user typed 'streamlit --help' instead of 'streamlit help'.
import sys
assert len(sys.argv) == 2 # This is always true, but let's assert anyway.
sys.argv[1] = "--help"
main()
@main.command("version")
@click.pass_context
def main_version(ctx):
"""Print Streamlit's version number."""
# Pretend user typed 'streamlit --version' instead of 'streamlit version'
import sys
assert len(sys.argv) == 2 # This is always true, but let's assert anyway.
sys.argv[1] = "--version"
main()
@main.command("docs")
def main_docs():
"""Show help in browser."""
print("Showing help page in browser...")
from streamlit import util
util.open_browser("https://docs.streamlit.io")
@main.command("hello")
@configurator_options
def main_hello(**kwargs):
"""Runs the Hello World script."""
from streamlit.hello import hello
bootstrap.load_config_options(flag_options=kwargs)
filename = hello.__file__
_main_run(filename, flag_options=kwargs)
@main.command("run")
@configurator_options
@click.argument("target", required=True, envvar="STREAMLIT_RUN_TARGET")
@click.argument("args", nargs=-1)
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
bootstrap.load_config_options(flag_options=kwargs)
_, extension = os.path.splitext(target)
if extension[1:] not in ACCEPTED_FILE_EXTENSIONS:
if extension[1:] == "":
raise click.BadArgumentUsage(
"Streamlit requires raw Python (.py) files, but the provided file has no extension.\nFor more information, please see https://docs.streamlit.io"
)
else:
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, flag_options=kwargs)
else:
if not os.path.exists(target):
raise click.BadParameter("File does not exist: {}".format(target))
_main_run(target, args, flag_options=kwargs)
def _get_command_line_as_string() -> Optional[str]:
import subprocess
parent = click.get_current_context().parent
if parent is None:
return None
cmd_line_as_list = [parent.command_path]
cmd_line_as_list.extend(click.get_os_args())
return subprocess.list2cmdline(cmd_line_as_list)
def _main_run(file, args=None, flag_options=None):
if args is None:
args = []
if flag_options is None:
flag_options = {}
command_line = _get_command_line_as_string()
# Set a global flag indicating that we're "within" streamlit.
streamlit._is_running_with_streamlit = True
check_credentials()
bootstrap.run(file, command_line, args, flag_options)
# SUBCOMMAND: cache
@main.group("cache")
def cache():
"""Manage the Streamlit cache."""
pass
@cache.command("clear")
def cache_clear():
"""Clear st.cache, st.memo, and st.singleton caches."""
import streamlit.legacy_caching
import streamlit.caching
result = streamlit.legacy_caching.clear_cache()
cache_path = streamlit.legacy_caching.get_cache_path()
if result:
print("Cleared directory %s." % cache_path)
else:
print("Nothing to clear at %s." % cache_path)
streamlit.caching.clear_memo_cache()
streamlit.caching.clear_singleton_cache()
# SUBCOMMAND: config
@main.group("config")
def config():
"""Manage Streamlit's config settings."""
pass
@config.command("show")
@configurator_options
def config_show(**kwargs):
"""Show all of Streamlit's config settings."""
bootstrap.load_config_options(flag_options=kwargs)
_config.show_config()
# SUBCOMMAND: activate
@main.group("activate", invoke_without_command=True)
@click.pass_context
def activate(ctx):
"""Activate Streamlit by entering your email."""
if not ctx.invoked_subcommand:
Credentials.get_current().activate()
@activate.command("reset")
def activate_reset():
"""Reset Activation Credentials."""
Credentials.get_current().reset()
if __name__ == "__main__":
main()