16
16
"""Allows us to create and absorb changes (aka Deltas) to elements."""
17
17
18
18
# Python 2/3 compatibility
19
- from __future__ import print_function , division , unicode_literals , \
20
- absolute_import
19
+ from __future__ import print_function , division , unicode_literals , absolute_import
21
20
from streamlit .compatibility import setup_2_3_shims
22
21
23
22
setup_2_3_shims (globals ())
31
30
from datetime import date
32
31
from datetime import time
33
32
33
+ from streamlit import caching
34
34
from streamlit import metrics
35
35
from streamlit .proto import Balloons_pb2
36
36
from streamlit .proto import BlockPath_pb2
@@ -104,6 +104,9 @@ def _with_element(method):
104
104
105
105
@_wraps_with_cleaned_sig (method , 2 ) # Remove self and element from sig.
106
106
def wrapped_method (dg , * args , ** kwargs ):
107
+ # Warn if we're called from within an @st.cache function
108
+ caching .maybe_show_cached_st_function_warning (dg )
109
+
107
110
delta_type = method .__name__
108
111
last_index = - 1
109
112
@@ -214,22 +217,24 @@ def __init__(self,
214
217
215
218
def __getattr__ (self , name ):
216
219
import streamlit as st
217
- streamlit_methods = [method_name for method_name in dir (st )
218
- if callable (getattr (st , method_name ))]
220
+
221
+ streamlit_methods = [
222
+ method_name for method_name in dir (st ) if callable (getattr (st , method_name ))
223
+ ]
219
224
220
225
def wrapper (* args , ** kwargs ):
221
226
if name in streamlit_methods :
222
227
if self ._container == BlockPath_pb2 .BlockPath .SIDEBAR :
223
- message = "Method `%(name)s()` does not exist for " \
224
- "`st.sidebar`. Did you mean `st. %(name)s()`?" % {
225
- "name" : name
226
- }
228
+ message = (
229
+ "Method ` %(name)s()` does not exist for "
230
+ "`st.sidebar`. Did you mean `st.%(name)s()`?" % { "name" : name }
231
+ )
227
232
else :
228
- message = "Method `%(name)s()` does not exist for " \
229
- "`DeltaGenerator` objects. Did you mean " \
230
- "`st.%(name)s()`?" % {
231
- "name" : name
232
- }
233
+ message = (
234
+ "Method `%(name)s()` does not exist for "
235
+ "`DeltaGenerator` objects. Did you mean "
236
+ "`st.%(name)s()`?" % { "name" : name }
237
+ )
233
238
else :
234
239
message = "`%(name)s()` is not a valid Streamlit command." % {
235
240
"name" : name
@@ -682,8 +687,7 @@ def exception(self, element, exception, exception_traceback=None):
682
687
"""
683
688
import streamlit .elements .exception_proto as exception_proto
684
689
685
- exception_proto .marshall (element .exception , exception ,
686
- exception_traceback )
690
+ exception_proto .marshall (element .exception , exception , exception_traceback )
687
691
688
692
@_with_element
689
693
def _text_exception (self , element , exception_type , message , stack_trace ):
@@ -858,8 +862,7 @@ def bar_chart(self, element, data=None, width=0, height=0):
858
862
altair .marshall (element .vega_lite_chart , chart , width , height = height )
859
863
860
864
@_with_element
861
- def vega_lite_chart (self , element , data = None , spec = None , width = 0 ,
862
- ** kwargs ):
865
+ def vega_lite_chart (self , element , data = None , spec = None , width = 0 , ** kwargs ):
863
866
"""Display a chart using the Vega-Lite library.
864
867
865
868
Parameters
@@ -914,8 +917,7 @@ def vega_lite_chart(self, element, data=None, spec=None, width=0,
914
917
"""
915
918
import streamlit .elements .vega_lite as vega_lite
916
919
917
- vega_lite .marshall (element .vega_lite_chart , data , spec , width ,
918
- ** kwargs )
920
+ vega_lite .marshall (element .vega_lite_chart , data , spec , width , ** kwargs )
919
921
920
922
@_with_element
921
923
def altair_chart (self , element , altair_chart , width = 0 ):
@@ -1031,8 +1033,7 @@ def graphviz_chart(self, element, figure_or_dot, width=0, height=0):
1031
1033
1032
1034
@_with_element
1033
1035
def plotly_chart (
1034
- self , element , figure_or_data , width = 0 , height = 0 , sharing = "streamlit" ,
1035
- ** kwargs
1036
+ self , element , figure_or_data , width = 0 , height = 0 , sharing = "streamlit" , ** kwargs
1036
1037
):
1037
1038
"""Display an interactive Plotly chart.
1038
1039
@@ -1111,8 +1112,7 @@ def plotly_chart(
1111
1112
import streamlit .elements .plotly_chart as plotly_chart
1112
1113
1113
1114
plotly_chart .marshall (
1114
- element .plotly_chart , figure_or_data , width , height , sharing ,
1115
- ** kwargs
1115
+ element .plotly_chart , figure_or_data , width , height , sharing , ** kwargs
1116
1116
)
1117
1117
1118
1118
@_with_element
@@ -1395,8 +1395,7 @@ def checkbox(self, element, ui_value, label, value=False):
1395
1395
return current_value
1396
1396
1397
1397
@_widget
1398
- def multiselect (self , element , ui_value , label , options ,
1399
- format_func = str ):
1398
+ def multiselect (self , element , ui_value , label , options , format_func = str ):
1400
1399
"""Display a multiselect widget.
1401
1400
The multiselect widget starts as empty.
1402
1401
@@ -1430,13 +1429,11 @@ def multiselect(self, element, ui_value, label, options,
1430
1429
1431
1430
element .multiselect .label = label
1432
1431
element .multiselect .default [:] = current_value
1433
- element .multiselect .options [:] = [str (format_func (opt )) for opt in
1434
- options ]
1432
+ element .multiselect .options [:] = [str (format_func (opt )) for opt in options ]
1435
1433
return [options [i ] for i in current_value ]
1436
1434
1437
1435
@_widget
1438
- def radio (self , element , ui_value , label , options , index = 0 ,
1439
- format_func = str ):
1436
+ def radio (self , element , ui_value , label , options , index = 0 , format_func = str ):
1440
1437
"""Display a radio button widget.
1441
1438
1442
1439
Parameters
@@ -1470,12 +1467,10 @@ def radio(self, element, ui_value, label, options, index=0,
1470
1467
1471
1468
"""
1472
1469
if not isinstance (index , int ):
1473
- raise TypeError (
1474
- "Radio Value has invalid type: %s" % type (index ).__name__ )
1470
+ raise TypeError ("Radio Value has invalid type: %s" % type (index ).__name__ )
1475
1471
1476
1472
if len (options ) and not 0 <= index < len (options ):
1477
- raise ValueError (
1478
- "Radio index must be between 0 and length of options" )
1473
+ raise ValueError ("Radio index must be between 0 and length of options" )
1479
1474
1480
1475
current_value = ui_value if ui_value is not None else index
1481
1476
@@ -1485,8 +1480,7 @@ def radio(self, element, ui_value, label, options, index=0,
1485
1480
return options [current_value ] if len (options ) else NoValue
1486
1481
1487
1482
@_widget
1488
- def selectbox (self , element , ui_value , label , options , index = 0 ,
1489
- format_func = str ):
1483
+ def selectbox (self , element , ui_value , label , options , index = 0 , format_func = str ):
1490
1484
"""Display a select widget.
1491
1485
1492
1486
Parameters
@@ -1522,15 +1516,13 @@ def selectbox(self, element, ui_value, label, options, index=0,
1522
1516
)
1523
1517
1524
1518
if len (options ) and not 0 <= index < len (options ):
1525
- raise ValueError (
1526
- "Selectbox index must be between 0 and length of options" )
1519
+ raise ValueError ("Selectbox index must be between 0 and length of options" )
1527
1520
1528
1521
current_value = ui_value if ui_value is not None else index
1529
1522
1530
1523
element .selectbox .label = label
1531
1524
element .selectbox .value = current_value
1532
- element .selectbox .options [:] = [str (format_func (opt )) for opt in
1533
- options ]
1525
+ element .selectbox .options [:] = [str (format_func (opt )) for opt in options ]
1534
1526
return options [current_value ] if len (options ) else NoValue
1535
1527
1536
1528
@_widget
@@ -1566,7 +1558,7 @@ def slider(
1566
1558
Defaults to 1 if the value is an int, 0.01 otherwise.
1567
1559
format : str or None
1568
1560
Printf/Python format string.
1569
-
1561
+
1570
1562
1571
1563
Returns
1572
1564
-------
@@ -1665,8 +1657,7 @@ def slider(
1665
1657
else :
1666
1658
start , end = value
1667
1659
if not min_value <= start <= end <= max_value :
1668
- raise ValueError (
1669
- "The value and/or arguments are out of range." )
1660
+ raise ValueError ("The value and/or arguments are out of range." )
1670
1661
1671
1662
# Convert the current value to the appropriate type.
1672
1663
current_value = ui_value if ui_value is not None else value
@@ -1688,18 +1679,17 @@ def slider(
1688
1679
else :
1689
1680
format = "%0.2f"
1690
1681
# It would be great if we could guess the number of decimal places from
1691
- # the step`argument, but this would only be meaningful if step were a decimal.
1692
- # As a possible improvement we could make this function accept decimals
1682
+ # the step`argument, but this would only be meaningful if step were a decimal.
1683
+ # As a possible improvement we could make this function accept decimals
1693
1684
# and/or use some heuristics for floats.
1694
-
1685
+
1695
1686
element .slider .label = label
1696
- element .slider .value [:] = [
1697
- current_value ] if single_value else current_value
1687
+ element .slider .value [:] = [current_value ] if single_value else current_value
1698
1688
element .slider .min = min_value
1699
1689
element .slider .max = max_value
1700
1690
element .slider .step = step
1701
- element .slider .format = format
1702
-
1691
+ element .slider .format = format
1692
+
1703
1693
return current_value if single_value else tuple (current_value )
1704
1694
1705
1695
@_widget
@@ -1795,8 +1785,7 @@ def time_input(self, element, ui_value, label, value=None):
1795
1785
1796
1786
# Ensure that the value is either datetime/time
1797
1787
if not isinstance (value , datetime ) and not isinstance (value , time ):
1798
- raise TypeError (
1799
- "The type of the value should be either datetime or time." )
1788
+ raise TypeError ("The type of the value should be either datetime or time." )
1800
1789
1801
1790
# Convert datetime to time
1802
1791
if isinstance (value , datetime ):
@@ -1842,8 +1831,7 @@ def date_input(self, element, ui_value, label, value=None):
1842
1831
1843
1832
# Ensure that the value is either datetime/time
1844
1833
if not isinstance (value , datetime ) and not isinstance (value , date ):
1845
- raise TypeError (
1846
- "The type of the value should be either datetime or date." )
1834
+ raise TypeError ("The type of the value should be either datetime or date." )
1847
1835
1848
1836
# Convert datetime to date
1849
1837
if isinstance (value , datetime ):
0 commit comments