Ipywidgets intro
Import libraries¶
In [1]:
Copied!
# !pip install geodemo
# !pip install geodemo
In [2]:
Copied!
import ee
import geodemo
import ipywidgets as widgets
from ipyleaflet import WidgetControl
import ee
import geodemo
import ipywidgets as widgets
from ipyleaflet import WidgetControl
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[2], line 2 1 import ee ----> 2 import geodemo 3 import ipywidgets as widgets 4 from ipyleaflet import WidgetControl ModuleNotFoundError: No module named 'geodemo'
Create an interactive map¶
In [3]:
Copied!
Map = geodemo.Map()
Map
Map = geodemo.Map()
Map
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 Map = geodemo.Map() 2 Map NameError: name 'geodemo' is not defined
In [4]:
Copied!
geodemo.ee_initialize()
geodemo.ee_initialize()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 geodemo.ee_initialize() NameError: name 'geodemo' is not defined
In [5]:
Copied!
dem = ee.Image('USGS/SRTMGL1_003')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}
Map.addLayer(dem, vis_params, 'DEM')
dem = ee.Image('USGS/SRTMGL1_003')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}
Map.addLayer(dem, vis_params, 'DEM')
--------------------------------------------------------------------------- EEException Traceback (most recent call last) Cell In[5], line 1 ----> 1 dem = ee.Image('USGS/SRTMGL1_003') 3 vis_params = { 4 'min': 0, 5 'max': 4000, 6 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']} 8 Map.addLayer(dem, vis_params, 'DEM') File ~\miniconda3\envs\demo311\Lib\site-packages\ee\computedobject.py:29, in ComputedObjectMetaclass.__call__(cls, *args, **kwargs) 27 return args[0] 28 else: ---> 29 return type.__call__(cls, *args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\deprecation.py:127, in WarnForDeprecatedAsset.<locals>.Decorator.<locals>.Wrapper(*args, **kwargs) 125 if asset: 126 _IssueAssetDeprecationWarning(asset) --> 127 return func(*args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\image.py:73, in Image.__init__(self, args, version) 54 @deprecation.WarnForDeprecatedAsset('args') 55 def __init__(self, args: Any | None = None, version: float | None = None): 56 """Constructs an Earth Engine image. 57 58 Args: (...) 71 EEException: if passed something other than the above. 72 """ ---> 73 self.initialize() 75 if version is not None: 76 if ee_types.isString(args) and ee_types.isNumber(version): 77 # An ID and version. File ~\miniconda3\envs\demo311\Lib\site-packages\ee\image.py:119, in Image.initialize(cls) 117 """Imports API functions to this class.""" 118 if not cls._initialized: --> 119 apifunction.ApiFunction.importApi(cls, cls.name(), cls.name()) 120 cls._initialized = True File ~\miniconda3\envs\demo311\Lib\site-packages\ee\_utils.py:39, in accept_opt_prefix.<locals>.opt_fixed.<locals>.wrapper(*args, **kwargs) 37 if new_key not in kwargs: 38 kwargs[new_key] = old_key_val ---> 39 return func(*args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\apifunction.py:199, in ApiFunction.importApi(cls, target, prefix, type_name, prepend) 179 @classmethod 180 @_utils.accept_opt_prefix('opt_prepend') 181 def importApi( (...) 186 prepend: str | None = None, 187 ) -> None: 188 """Adds all API functions that begin with a given prefix to a target class. 189 190 Args: (...) 197 functions. 198 """ --> 199 cls.initialize() 200 prepend = prepend or '' 201 for name, api_func in cls._api.items(): File ~\miniconda3\envs\demo311\Lib\site-packages\ee\apifunction.py:163, in ApiFunction.initialize(cls) 161 """Initializes the list of signatures from the Earth Engine front-end.""" 162 if not cls._api: --> 163 signatures = data.getAlgorithms() 164 api = {} 165 for name, sig in signatures.items(): 166 # Strip type parameters. File ~\miniconda3\envs\demo311\Lib\site-packages\ee\data.py:1421, in getAlgorithms() 1404 """Get the list of algorithms. 1405 1406 Returns: (...) 1417 is not specified. 1418 """ 1419 try: 1420 call = ( -> 1421 _get_cloud_projects() 1422 .algorithms() 1423 .list(parent=_get_projects_path(), prettyPrint=False) 1424 ) 1425 except TypeError: 1426 call = ( 1427 _get_cloud_projects() 1428 .algorithms() 1429 .list(project=_get_projects_path(), prettyPrint=False) 1430 ) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\data.py:289, in _get_cloud_projects() 287 state = _get_state() 288 if state.cloud_api_resource is None: --> 289 raise ee_exception.EEException(_NOT_INITIALIZED_MESSAGE) 290 return state.cloud_api_resource.projects() EEException: Earth Engine client library not initialized. See http://goo.gle/ee-auth.
Add vector data¶
In [6]:
Copied!
fc = ee.FeatureCollection('TIGER/2018/States')
Map.addLayer(fc, {}, 'US States')
fc = ee.FeatureCollection('TIGER/2018/States')
Map.addLayer(fc, {}, 'US States')
--------------------------------------------------------------------------- EEException Traceback (most recent call last) Cell In[6], line 1 ----> 1 fc = ee.FeatureCollection('TIGER/2018/States') 2 Map.addLayer(fc, {}, 'US States') File ~\miniconda3\envs\demo311\Lib\site-packages\ee\computedobject.py:29, in ComputedObjectMetaclass.__call__(cls, *args, **kwargs) 27 return args[0] 28 else: ---> 29 return type.__call__(cls, *args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\_utils.py:39, in accept_opt_prefix.<locals>.opt_fixed.<locals>.wrapper(*args, **kwargs) 37 if new_key not in kwargs: 38 kwargs[new_key] = old_key_val ---> 39 return func(*args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\deprecation.py:127, in WarnForDeprecatedAsset.<locals>.Decorator.<locals>.Wrapper(*args, **kwargs) 125 if asset: 126 _IssueAssetDeprecationWarning(asset) --> 127 return func(*args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\featurecollection.py:63, in FeatureCollection.__init__(self, args, column) 33 @_utils.accept_opt_prefix('opt_column') 34 @deprecation.WarnForDeprecatedAsset('args') 35 def __init__( (...) 45 column: Any | None = None, 46 ): 47 """Constructs a collection features. 48 49 Args: (...) 61 EEException: if passed something other than the above. 62 """ ---> 63 self.initialize() 65 # Wrap geometries with features. 66 if isinstance(args, geometry.Geometry): File ~\miniconda3\envs\demo311\Lib\site-packages\ee\featurecollection.py:109, in FeatureCollection.initialize(cls) 107 """Imports API functions to this class.""" 108 if not cls._initialized: --> 109 super().initialize() 110 apifunction.ApiFunction.importApi(cls, cls.name(), cls.name()) 111 cls._initialized = True File ~\miniconda3\envs\demo311\Lib\site-packages\ee\collection.py:57, in Collection.initialize(cls) 55 """Imports API functions to this class.""" 56 if not cls._initialized: ---> 57 apifunction.ApiFunction.importApi(cls, cls.name(), cls.name()) 58 apifunction.ApiFunction.importApi( 59 cls, 'AggregateFeatureCollection', cls.name(), 'aggregate_') 60 cls._initialized = True File ~\miniconda3\envs\demo311\Lib\site-packages\ee\_utils.py:39, in accept_opt_prefix.<locals>.opt_fixed.<locals>.wrapper(*args, **kwargs) 37 if new_key not in kwargs: 38 kwargs[new_key] = old_key_val ---> 39 return func(*args, **kwargs) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\apifunction.py:199, in ApiFunction.importApi(cls, target, prefix, type_name, prepend) 179 @classmethod 180 @_utils.accept_opt_prefix('opt_prepend') 181 def importApi( (...) 186 prepend: str | None = None, 187 ) -> None: 188 """Adds all API functions that begin with a given prefix to a target class. 189 190 Args: (...) 197 functions. 198 """ --> 199 cls.initialize() 200 prepend = prepend or '' 201 for name, api_func in cls._api.items(): File ~\miniconda3\envs\demo311\Lib\site-packages\ee\apifunction.py:163, in ApiFunction.initialize(cls) 161 """Initializes the list of signatures from the Earth Engine front-end.""" 162 if not cls._api: --> 163 signatures = data.getAlgorithms() 164 api = {} 165 for name, sig in signatures.items(): 166 # Strip type parameters. File ~\miniconda3\envs\demo311\Lib\site-packages\ee\data.py:1421, in getAlgorithms() 1404 """Get the list of algorithms. 1405 1406 Returns: (...) 1417 is not specified. 1418 """ 1419 try: 1420 call = ( -> 1421 _get_cloud_projects() 1422 .algorithms() 1423 .list(parent=_get_projects_path(), prettyPrint=False) 1424 ) 1425 except TypeError: 1426 call = ( 1427 _get_cloud_projects() 1428 .algorithms() 1429 .list(project=_get_projects_path(), prettyPrint=False) 1430 ) File ~\miniconda3\envs\demo311\Lib\site-packages\ee\data.py:289, in _get_cloud_projects() 287 state = _get_state() 288 if state.cloud_api_resource is None: --> 289 raise ee_exception.EEException(_NOT_INITIALIZED_MESSAGE) 290 return state.cloud_api_resource.projects() EEException: Earth Engine client library not initialized. See http://goo.gle/ee-auth.
Change layer opacity¶
In [7]:
Copied!
Map
Map
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 Map NameError: name 'Map' is not defined
In [8]:
Copied!
Map.layers
Map.layers
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 Map.layers NameError: name 'Map' is not defined
In [9]:
Copied!
dem_layer = Map.layers[2]
dem_layer.interact(opacity=(0, 1, 0.1))
dem_layer = Map.layers[2]
dem_layer.interact(opacity=(0, 1, 0.1))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 1 ----> 1 dem_layer = Map.layers[2] 2 dem_layer.interact(opacity=(0, 1, 0.1)) NameError: name 'Map' is not defined
In [10]:
Copied!
vector_layer = Map.layers[3]
vector_layer.interact(opacity=(0, 1, 0.1))
vector_layer = Map.layers[3]
vector_layer.interact(opacity=(0, 1, 0.1))
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[10], line 1 ----> 1 vector_layer = Map.layers[3] 2 vector_layer.interact(opacity=(0, 1, 0.1)) NameError: name 'Map' is not defined
Widget list¶
Widget list: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html
Icons: https://fontawesome.com/v4.7.0/icons
Numeric widgets¶
IntSlider¶
In [11]:
Copied!
int_slider = widgets.IntSlider(
value=2000,
min=1984,
max=2020,
step=1,
description='Year:'
)
int_slider
int_slider = widgets.IntSlider(
value=2000,
min=1984,
max=2020,
step=1,
description='Year:'
)
int_slider
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[11], line 1 ----> 1 int_slider = widgets.IntSlider( 2 value=2000, 3 min=1984, 4 max=2020, 5 step=1, 6 description='Year:' 7 ) 8 int_slider NameError: name 'widgets' is not defined
In [12]:
Copied!
int_slider.value
int_slider.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[12], line 1 ----> 1 int_slider.value NameError: name 'int_slider' is not defined
FloatSlider¶
In [13]:
Copied!
float_slider = widgets.FloatSlider(
value=0,
min=-1,
max=1,
step=0.05,
description='Threshold:'
)
float_slider
float_slider = widgets.FloatSlider(
value=0,
min=-1,
max=1,
step=0.05,
description='Threshold:'
)
float_slider
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[13], line 1 ----> 1 float_slider = widgets.FloatSlider( 2 value=0, 3 min=-1, 4 max=1, 5 step=0.05, 6 description='Threshold:' 7 ) 8 float_slider NameError: name 'widgets' is not defined
In [14]:
Copied!
float_slider.value
float_slider.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[14], line 1 ----> 1 float_slider.value NameError: name 'float_slider' is not defined
IntProgress¶
In [15]:
Copied!
int_progress = widgets.IntProgress(
value=7,
min=0,
max=10,
step=1,
description='Loading:',
bar_style='', # 'success', 'info', 'warning', 'danger' or ''
orientation='horizontal'
)
int_progress
int_progress = widgets.IntProgress(
value=7,
min=0,
max=10,
step=1,
description='Loading:',
bar_style='', # 'success', 'info', 'warning', 'danger' or ''
orientation='horizontal'
)
int_progress
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[15], line 1 ----> 1 int_progress = widgets.IntProgress( 2 value=7, 3 min=0, 4 max=10, 5 step=1, 6 description='Loading:', 7 bar_style='', # 'success', 'info', 'warning', 'danger' or '' 8 orientation='horizontal' 9 ) 10 int_progress NameError: name 'widgets' is not defined
In [16]:
Copied!
int_text = widgets.IntText(
value=7,
description='Any:',
)
int_text
int_text = widgets.IntText(
value=7,
description='Any:',
)
int_text
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[16], line 1 ----> 1 int_text = widgets.IntText( 2 value=7, 3 description='Any:', 4 ) 5 int_text NameError: name 'widgets' is not defined
In [17]:
Copied!
float_text = widgets.FloatText(
value=7.5,
description='Any:',
)
float_text
float_text = widgets.FloatText(
value=7.5,
description='Any:',
)
float_text
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[17], line 1 ----> 1 float_text = widgets.FloatText( 2 value=7.5, 3 description='Any:', 4 ) 5 float_text NameError: name 'widgets' is not defined
In [18]:
Copied!
toggle_button = widgets.ToggleButton(
value=False,
description='Click me',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
toggle_button
toggle_button = widgets.ToggleButton(
value=False,
description='Click me',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
toggle_button
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[18], line 1 ----> 1 toggle_button = widgets.ToggleButton( 2 value=False, 3 description='Click me', 4 disabled=False, 5 button_style='success', # 'success', 'info', 'warning', 'danger' or '' 6 tooltip='Description', 7 icon='check' # (FontAwesome names without the `fa-` prefix) 8 ) 9 toggle_button NameError: name 'widgets' is not defined
In [19]:
Copied!
toggle_button.value
toggle_button.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[19], line 1 ----> 1 toggle_button.value NameError: name 'toggle_button' is not defined
Checkbox¶
In [20]:
Copied!
checkbox = widgets.Checkbox(
value=False,
description='Check me',
disabled=False,
indent=False
)
checkbox
checkbox = widgets.Checkbox(
value=False,
description='Check me',
disabled=False,
indent=False
)
checkbox
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[20], line 1 ----> 1 checkbox = widgets.Checkbox( 2 value=False, 3 description='Check me', 4 disabled=False, 5 indent=False 6 ) 7 checkbox NameError: name 'widgets' is not defined
In [21]:
Copied!
checkbox.value
checkbox.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[21], line 1 ----> 1 checkbox.value NameError: name 'checkbox' is not defined
In [22]:
Copied!
dropdown = widgets.Dropdown(
options=['USA', 'Canada', 'Mexico'],
value='Canada',
description='Country:'
)
dropdown
dropdown = widgets.Dropdown(
options=['USA', 'Canada', 'Mexico'],
value='Canada',
description='Country:'
)
dropdown
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[22], line 1 ----> 1 dropdown = widgets.Dropdown( 2 options=['USA', 'Canada', 'Mexico'], 3 value='Canada', 4 description='Country:' 5 ) 6 dropdown NameError: name 'widgets' is not defined
In [23]:
Copied!
dropdown.value
dropdown.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[23], line 1 ----> 1 dropdown.value NameError: name 'dropdown' is not defined
RadioButtons¶
In [24]:
Copied!
radio_buttons = widgets.RadioButtons(
options=['USA', 'Canada', 'Mexico'],
value='Canada',
description='Country:'
)
radio_buttons
radio_buttons = widgets.RadioButtons(
options=['USA', 'Canada', 'Mexico'],
value='Canada',
description='Country:'
)
radio_buttons
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[24], line 1 ----> 1 radio_buttons = widgets.RadioButtons( 2 options=['USA', 'Canada', 'Mexico'], 3 value='Canada', 4 description='Country:' 5 ) 6 radio_buttons NameError: name 'widgets' is not defined
In [25]:
Copied!
radio_buttons.value
radio_buttons.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[25], line 1 ----> 1 radio_buttons.value NameError: name 'radio_buttons' is not defined
In [26]:
Copied!
text = widgets.Text(
value='USA',
placeholder='Enter a country name',
description='Country:',
disabled=False
)
text
text = widgets.Text(
value='USA',
placeholder='Enter a country name',
description='Country:',
disabled=False
)
text
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[26], line 1 ----> 1 text = widgets.Text( 2 value='USA', 3 placeholder='Enter a country name', 4 description='Country:', 5 disabled=False 6 ) 7 text NameError: name 'widgets' is not defined
In [27]:
Copied!
text.value
text.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[27], line 1 ----> 1 text.value NameError: name 'text' is not defined
Textarea¶
In [28]:
Copied!
widgets.Textarea(
value='Hello World',
placeholder='Type something',
description='String:',
disabled=False
)
widgets.Textarea(
value='Hello World',
placeholder='Type something',
description='String:',
disabled=False
)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[28], line 1 ----> 1 widgets.Textarea( 2 value='Hello World', 3 placeholder='Type something', 4 description='String:', 5 disabled=False 6 ) NameError: name 'widgets' is not defined
HTML¶
In [29]:
Copied!
widgets.HTML(
value="Hello <b>World</b>",
placeholder='Some HTML',
description='Some HTML',
)
widgets.HTML(
value="Hello World",
placeholder='Some HTML',
description='Some HTML',
)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[29], line 1 ----> 1 widgets.HTML( 2 value="Hello <b>World</b>", 3 placeholder='Some HTML', 4 description='Some HTML', 5 ) NameError: name 'widgets' is not defined
In [30]:
Copied!
widgets.HTML(
value='<img src="https://earthengine.google.com/static/images/earth-engine-logo.png" width="100" height="100">'
)
widgets.HTML(
value='
'
)
'
)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[30], line 1 ----> 1 widgets.HTML( 2 value='<img src="https://earthengine.google.com/static/images/earth-engine-logo.png" width="100" height="100">' 3 ) NameError: name 'widgets' is not defined
Button¶
In [31]:
Copied!
button = widgets.Button(
description='Click me',
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
button
button = widgets.Button(
description='Click me',
button_style='info', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Click me',
icon='check' # (FontAwesome names without the `fa-` prefix)
)
button
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[31], line 1 ----> 1 button = widgets.Button( 2 description='Click me', 3 button_style='info', # 'success', 'info', 'warning', 'danger' or '' 4 tooltip='Click me', 5 icon='check' # (FontAwesome names without the `fa-` prefix) 6 ) 7 button NameError: name 'widgets' is not defined
Date picker¶
In [32]:
Copied!
date_picker = widgets.DatePicker(
description='Pick a Date',
disabled=False
)
date_picker
date_picker = widgets.DatePicker(
description='Pick a Date',
disabled=False
)
date_picker
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[32], line 1 ----> 1 date_picker = widgets.DatePicker( 2 description='Pick a Date', 3 disabled=False 4 ) 5 date_picker NameError: name 'widgets' is not defined
In [33]:
Copied!
date_picker.value
date_picker.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[33], line 1 ----> 1 date_picker.value NameError: name 'date_picker' is not defined
Color picker¶
In [34]:
Copied!
color_picker = widgets.ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
color_picker
color_picker = widgets.ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
color_picker
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[34], line 1 ----> 1 color_picker = widgets.ColorPicker( 2 concise=False, 3 description='Pick a color', 4 value='blue', 5 disabled=False 6 ) 7 color_picker NameError: name 'widgets' is not defined
In [35]:
Copied!
color_picker.value
color_picker.value
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[35], line 1 ----> 1 color_picker.value NameError: name 'color_picker' is not defined
Output widget¶
In [36]:
Copied!
out = widgets.Output(layout={'border': '1px solid black'})
out
out = widgets.Output(layout={'border': '1px solid black'})
out
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[36], line 1 ----> 1 out = widgets.Output(layout={'border': '1px solid black'}) 2 out NameError: name 'widgets' is not defined
In [37]:
Copied!
with out:
for i in range(10):
print(i, 'Hello world!')
with out:
for i in range(10):
print(i, 'Hello world!')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[37], line 1 ----> 1 with out: 2 for i in range(10): 3 print(i, 'Hello world!') NameError: name 'out' is not defined
In [38]:
Copied!
from IPython.display import YouTubeVideo
out.clear_output()
with out:
display(YouTubeVideo('mA21Us_3m28'))
out
from IPython.display import YouTubeVideo
out.clear_output()
with out:
display(YouTubeVideo('mA21Us_3m28'))
out
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[38], line 2 1 from IPython.display import YouTubeVideo ----> 2 out.clear_output() 3 with out: 4 display(YouTubeVideo('mA21Us_3m28')) NameError: name 'out' is not defined
In [39]:
Copied!
out.clear_output()
with out:
display(widgets.IntSlider())
out
out.clear_output()
with out:
display(widgets.IntSlider())
out
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[39], line 1 ----> 1 out.clear_output() 2 with out: 3 display(widgets.IntSlider()) NameError: name 'out' is not defined
Add a widget to the map¶
In [40]:
Copied!
Map = geodemo.Map()
dem = ee.Image('USGS/SRTMGL1_003')
fc = ee.FeatureCollection('TIGER/2018/States')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}
Map.addLayer(dem, vis_params, 'DEM')
Map.addLayer(fc, {}, 'US States')
Map
Map = geodemo.Map()
dem = ee.Image('USGS/SRTMGL1_003')
fc = ee.FeatureCollection('TIGER/2018/States')
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']}
Map.addLayer(dem, vis_params, 'DEM')
Map.addLayer(fc, {}, 'US States')
Map
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[40], line 1 ----> 1 Map = geodemo.Map() 3 dem = ee.Image('USGS/SRTMGL1_003') 4 fc = ee.FeatureCollection('TIGER/2018/States') NameError: name 'geodemo' is not defined
In [41]:
Copied!
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[41], line 1 ----> 1 output_widget = widgets.Output(layout={'border': '1px solid black'}) 2 output_control = WidgetControl(widget=output_widget, position='bottomright') 3 Map.add_control(output_control) NameError: name 'widgets' is not defined
In [42]:
Copied!
with output_widget:
print('Nice map!')
with output_widget:
print('Nice map!')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[42], line 1 ----> 1 with output_widget: 2 print('Nice map!') NameError: name 'output_widget' is not defined
In [43]:
Copied!
output_widget.clear_output()
logo = widgets.HTML(
value='<img src="https://earthengine.google.com/static/images/earth-engine-logo.png" width="100" height="100">'
)
with output_widget:
display(logo)
output_widget.clear_output()
logo = widgets.HTML(
value='
'
)
with output_widget:
display(logo)
'
)
with output_widget:
display(logo)--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[43], line 1 ----> 1 output_widget.clear_output() 2 logo = widgets.HTML( 3 value='<img src="https://earthengine.google.com/static/images/earth-engine-logo.png" width="100" height="100">' 4 ) 5 with output_widget: NameError: name 'output_widget' is not defined
In [44]:
Copied!
def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')
if kwargs.get('type') == 'click':
Map.default_style = {'cursor': 'wait'}
xy = ee.Geometry.Point(latlon[::-1])
selected_fc = fc.filterBounds(xy)
with output_widget:
output_widget.clear_output()
try:
name = selected_fc.first().get('NAME').getInfo()
usps = selected_fc.first().get('STUSPS').getInfo()
Map.layers = Map.layers[:4]
geom = selected_fc.geometry()
layer_name = name + '-' + usps
Map.addLayer(ee.Image().paint(geom, 0, 2), {'palette': 'red'}, layer_name)
print(layer_name)
except Exception as e:
print('No feature could be found')
Map.layers = Map.layers[:4]
Map.default_style = {'cursor': 'pointer'}
Map.on_interaction(handle_interaction)
def handle_interaction(**kwargs):
latlon = kwargs.get('coordinates')
if kwargs.get('type') == 'click':
Map.default_style = {'cursor': 'wait'}
xy = ee.Geometry.Point(latlon[::-1])
selected_fc = fc.filterBounds(xy)
with output_widget:
output_widget.clear_output()
try:
name = selected_fc.first().get('NAME').getInfo()
usps = selected_fc.first().get('STUSPS').getInfo()
Map.layers = Map.layers[:4]
geom = selected_fc.geometry()
layer_name = name + '-' + usps
Map.addLayer(ee.Image().paint(geom, 0, 2), {'palette': 'red'}, layer_name)
print(layer_name)
except Exception as e:
print('No feature could be found')
Map.layers = Map.layers[:4]
Map.default_style = {'cursor': 'pointer'}
Map.on_interaction(handle_interaction)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[44], line 25 21 Map.layers = Map.layers[:4] 23 Map.default_style = {'cursor': 'pointer'} ---> 25 Map.on_interaction(handle_interaction) NameError: name 'Map' is not defined