/
core.py
588 lines (487 loc) · 17.9 KB
/
core.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_core.ipynb.
# %% auto 0
__all__ = ['IN_IPYTHON', 'tqdm', 'StreamlitPatcher']
# %% ../nbs/01_core.ipynb 2
import functools
import json
import logging
import time
import typing as tp
from datetime import datetime
import IPython.display
import ipywidgets as widgets
import pandas as pd
import streamlit as st
from fastcore.basics import in_ipython, listify, noop, patch, patch_to
from fastcore.test import test_eq, test_fail
from IPython.utils.capture import capture_output
from .utils import test_md_output
# %% ../nbs/01_core.ipynb 3
from logging import getLogger
logger = getLogger(__name__)
# %% ../nbs/01_core.ipynb 4
# module obljects that we will be importing
IN_IPYTHON = in_ipython()
# %% ../nbs/01_core.ipynb 7
if IN_IPYTHON:
from tqdm.notebook import tqdm
else:
from stqdm import stqdm as tqdm
tqdm = tqdm # make this available in the module namespace
# %% ../nbs/01_core.ipynb 8
class StreamlitPatcher:
"""class to patch streamlit functions for displaying content in jupyter notebooks"""
def __init__(self):
self.is_registered: bool = False
self.registered_methods: tp.Set[str] = set()
def jupyter(self):
"""patches streamlit methods to display content in jupyter notebooks"""
# patch streamlit methods from MAPPING property dict
for method_name, wrapper in self.MAPPING.items():
self._wrap(method_name, wrapper)
self.is_registered = True
@staticmethod
def _get_streamlit_methods():
"""get all streamlit methods"""
return [attr for attr in dir(st) if not attr.startswith("_")]
# %% ../nbs/01_core.ipynb 9
@patch_to(StreamlitPatcher, cls_method=False)
def _wrap(
cls,
method_name: str,
wrapper: tp.Callable,
) -> None:
"""make a streamlit method jupyter friendly
Parameters
----------
method_name : str
which method to jupyterify
wrapper : tp.Callable
wrapper function to use
"""
if IN_IPYTHON: # only patch if in jupyter
trg = getattr(st, method_name) # get the streamlit method
setattr(st, method_name, wrapper(trg)) # patch the method
cls.registered_methods.add(method_name) # add to registered methods
# %% ../nbs/01_core.ipynb 13
def _display(arg: tp.Any) -> None:
if isinstance(arg, str):
IPython.display.display(IPython.display.Markdown(arg))
else:
IPython.display.display(arg)
def _st_write(func_to_decorate):
"""Decorator to display objects passed to Streamlit in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
for arg in args:
_display(arg)
return wrapper
# %% ../nbs/01_core.ipynb 19
def _st_heading(func_to_decorate: tp.Callable, tag: str) -> tp.Callable:
"""Decorator to display objects passed to Streamlit in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
body = args[0]
elif len(args) == 2:
body, anchor = args
elif len(args) > 2:
raise ValueError(
f"Too many positional arguments: {len(args)}, {func_to_decorate.__name__} only accepts 2"
)
elif len(args) == 0:
if "body" not in kwargs:
raise ValueError(
f"Missing required argument: body, {func_to_decorate.__name__} requires a body"
)
body = kwargs["body"]
if isinstance(body, str):
_display(f"{tag} {body}")
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts strings"
)
return wrapper
# %% ../nbs/01_core.ipynb 26
def _st_caption(func_to_decorate):
"""Decorator to display json"""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 0:
raise ValueError(f"at least one positional argument is required")
elif len(args) == 1:
body = args[0]
if isinstance(body, str):
body_caption = "\n".join([f"> {line}" for line in body.split("\n")])
_display(body_caption)
else:
raise TypeError(f"Unsupported type: {type(body)}")
return wrapper
# %% ../nbs/01_core.ipynb 30
def _st_type_check(
func_to_decorate: tp.Callable,
allowed_types: tp.Union[tp.Type, tp.Collection[tp.Type]],
) -> tp.Callable:
"""Decorator to display objects passed to Streamlit in Jupyter notebooks."""
allowed_types = listify(allowed_types) # make sure it's a list
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
body = args[0]
elif len(args) > 1:
raise ValueError(
f"Too many positional arguments: {len(args)}, {func_to_decorate.__name__} only accepts 2"
)
elif len(args) == 0:
if kwargs:
raise NotImplementedError(
f"kwargs not supported yet, 'streamlit_data_science.utils._wrap_st_type_check' only accepts positional arguments"
)
else:
raise ValueError(f"at least one positional argument is required")
if type(body) in allowed_types:
_display(body)
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts {allowed_types}"
)
return wrapper
# %% ../nbs/01_core.ipynb 34
def _jupyter_display_code(body: str, language: str = "python") -> None:
_display(f"```{language}\n{body}\n```")
# %% ../nbs/01_core.ipynb 36
def _st_code(func_to_decorate):
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
body = args[0]
language = kwargs["language"] if "language" in kwargs else "python"
elif len(args) == 2:
body, language = args
if isinstance(body, str):
_jupyter_display_code(body, language=language)
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts strings"
)
return wrapper
# %% ../nbs/01_core.ipynb 45
def _st_text(func_to_decorate):
"""Decorator to display mono-spaced text"""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 0:
raise ValueError(f"at least one positional argument is required")
elif len(args) == 1:
body = args[0]
elif len(args) >= 2:
raise ValueError("Only one positional argument is supported")
if isinstance(body, str):
_jupyter_display_code(body, language=None)
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts strings and dicts"
)
return wrapper
# %% ../nbs/01_core.ipynb 48
from IPython.display import Latex
def _st_latex(func_to_decorate):
"""Decorator to display latex equations"""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 0:
raise ValueError(f"at least one positional argument is required")
elif len(args) == 1:
body = args[0]
elif len(args) >= 2:
raise ValueError("Only one positional argument is supported")
if isinstance(body, str):
body = rf"\begin{{equation}}{body}\end{{equation}}"
display(Latex(body))
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts strings and dicts"
)
return wrapper
# %% ../nbs/01_core.ipynb 55
def _st_json(func_to_decorate):
"""Decorator to display json"""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 0:
raise ValueError(f"at least one positional argument is required")
elif len(args) == 1:
body = args[0]
expanded = kwargs.get("expanded", True)
elif len(args) >= 2:
raise ValueError("Only one positional argument is supported")
if isinstance(body, str) and not expanded:
_jupyter_display_code(body, language="json")
elif isinstance(body, str) and expanded:
body = json.dumps(json.loads(body), indent=2)
_jupyter_display_code(body, language="json")
elif isinstance(body, dict) and not expanded:
body = json.dumps(body)
_jupyter_display_code(body, language="json")
elif isinstance(body, dict) and expanded:
body = json.dumps(body, indent=2)
_jupyter_display_code(body, language="json")
else:
raise TypeError(
f"Unsupported type: {type(body)}, {func_to_decorate.__name__} only accepts strings and dicts"
)
return wrapper
# %% ../nbs/01_core.ipynb 65
def _dummy_wrapper_noop(func_to_decorate):
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
return noop # castrate the function to do nothing
return wrapper
# %% ../nbs/01_core.ipynb 73
class _DummyExpander:
__doc__ = st.expander.__doc__
def __init__(self, label: str, expanded: bool = False):
self.label = label
self.expanded = expanded
def __enter__(self):
_display(f">**expander starts**: {self.label}")
def __exit__(self, *args):
_display(f">**expander ends**")
def _st_expander(cls_to_replace: st.expander):
return _DummyExpander
# %% ../nbs/01_core.ipynb 77
def _st_text_input(func_to_decorate):
"""Decorator to display date input in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
description = args[0]
if "value" in kwargs:
value = kwargs["value"]
else:
value = None
elif len(args) == 2:
description, value = args
text = widgets.Textarea(
description=description,
value=value,
disabled=False,
placeholder="Type something",
)
display(text)
return text.value
return wrapper
# %% ../nbs/01_core.ipynb 82
def _st_date_input(func_to_decorate):
"""Decorator to display date input in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
description = args[0]
if "value" in kwargs:
value = pd.to_datetime(kwargs["value"]).date()
else:
value = datetime.now()
elif len(args) == 2:
description = args[0]
value = pd.to_datetime(args[1])
date = widgets.DatePicker(
description=description,
value=value,
disabled=False,
)
display(date)
return date.value
return wrapper
# %% ../nbs/01_core.ipynb 88
def _st_checkbox(func_to_decorate):
"""Decorator to display checkbox in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) == 1:
description = args[0]
if "value" in kwargs:
value = kwargs["value"]
else:
value = True
elif len(args) == 2:
description, value = args
w = widgets.Checkbox(
value=value, description=description, disabled=False, indent=False
)
display(w)
return w.value
return wrapper
# %% ../nbs/01_core.ipynb 93
def _st_single_choice(func_to_decorate, jupyter_widget: widgets.Widget):
"""Decorator to display single choice widget in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) < 1:
raise ValueError("You must provide at least 1 argument")
if len(args) == 1:
label = args[0]
options = kwargs["options"]
index = kwargs["index"] if "index" in kwargs else 0
elif len(args) == 2:
label, options = args
index = kwargs["index"] if "index" in kwargs else 0
elif len(args) == 3:
label, options, index = args
w = jupyter_widget(
options=options,
description=label,
index=index,
)
display(w)
return w.value
return wrapper
# %% ../nbs/01_core.ipynb 98
def _st_multiselect(func_to_decorate):
"""Decorator to display multiple choice widget in Jupyter notebooks."""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
if len(args) < 1:
raise ValueError("You must provide at least 1 argument")
if len(args) == 1:
label = args[0]
options = kwargs.get("options")
elif len(args) == 2:
label, options = args
index = kwargs["index"] if "index" in kwargs else 0
else:
raise ValueError("Too many positional arguments, provide at most 2")
w = widgets.SelectMultiple(
options=options,
description=label,
value=kwargs.get("default", []),
)
display(w)
return w.value
return wrapper
# %% ../nbs/01_core.ipynb 103
def _plot_metric(*, label, value, delta=None, label_visibility="visible"):
import plotly.graph_objects as go
if delta is None:
mode = "number"
template = {
"data": {
"indicator": [
{
"title": {"text": label},
}
]
}
}
else:
mode = "number+delta"
template = {
"data": {
"indicator": [
{
"title": {"text": label},
"delta": {"reference": value - delta},
}
]
}
}
fig = go.Figure()
fig.add_trace(
go.Indicator(
mode=mode,
value=value,
)
)
fig.update_layout(width=300, height=300, template=template)
if label_visibility != "hidden":
fig.show()
def _st_metric(func_to_decorate):
"""wrapper for st.metric"""
@functools.wraps(func_to_decorate)
def wrapper(*args, **kwargs):
# some unsupported kwargs, None by default
delta_color = kwargs.get("delta_color")
help = kwargs.get("help")
label_visibility = kwargs.get("label_visibility")
allowed_values = {
"label_visibility": ["visible", "hidden", "collapsed", None],
"delta_color": ["normal", "inverse", "off", None],
}
for k, v in allowed_values.items():
if not eval(f"{k} in v"):
got = eval(f"{k}")
raise ValueError(
f"f'{got}' is not an accepted value. {k} only accepts: {v}"
)
if len(args) == 0:
label = kwargs.get("label")
value = kwargs.get("value")
delta = kwargs.get("delta")
if len(args) == 1:
label = args[0]
value = kwargs.get("value")
delta = kwargs.get("delta")
elif len(args) == 2:
label, value = args
delta = kwargs.get("delta")
elif len(args) == 3:
label, value, delta = args
elif len(args) == 4:
label, value, delta, delta_color = args
elif len(args) == 5:
label, value, delta, delta_color, help = args
elif len(args) == 6:
label, value, delta, delta_color, help, label_visibility = args
else:
raise ValueError("Too many positional arguments, provide at most 6")
for kwarg in ["delta_color", "help", "label_visibility"]:
if eval(f"{kwarg} is not None"):
logger.warning(
f"`{kwarg}` argument is not supported in Jupyter notebooks, but will be applied in Streamlit"
)
try:
_plot_metric(label=label, value=value, delta=delta)
except ImportError:
msg = "plotly is not installed, falling back to default st.metric implementation\n"
msg += "To use plotly, run `pip install plotly`"
logger.warning(msg)
_display(f"`st.metric widget (this will work as expected in streamlit)`")
except Exception as e:
raise e
return wrapper
# %% ../nbs/01_core.ipynb 110
@patch_to(StreamlitPatcher, as_prop=True)
def MAPPING(cls) -> tp.Dict[str, tp.Callable]:
"""mapping of streamlit methods to their jupyter friendly versions"""
return {
"write": _st_write,
"title": functools.partial(_st_heading, tag="#"),
"header": functools.partial(_st_heading, tag="##"),
"subheader": functools.partial(_st_heading, tag="###"),
"caption": _st_caption,
"markdown": functools.partial(_st_type_check, allowed_types=str),
"dataframe": functools.partial(_st_type_check, allowed_types=pd.DataFrame),
"experimental_data_editor": functools.partial(
_st_type_check, allowed_types=pd.DataFrame
),
"date_input": _st_date_input,
"text": _st_text,
"latex": _st_latex,
"json": _st_json,
"cache": _dummy_wrapper_noop,
"cache_data": _dummy_wrapper_noop,
"cache_resource": _dummy_wrapper_noop,
"expander": _st_expander,
"text_input": _st_text_input,
"text_area": _st_text_input,
"code": _st_code,
"checkbox": _st_checkbox,
"radio": functools.partial(
_st_single_choice, jupyter_widget=widgets.RadioButtons
),
"selectbox": functools.partial(
_st_single_choice, jupyter_widget=widgets.Dropdown
),
"multiselect": _st_multiselect,
"metric": _st_metric,
}