U
    ꥡc$                     @   s   d dl Z d dlZddlmZ ddlmZ ddlmZ ddlmZ	 ddl
mZ dd	lmZ dd
lmZ ddlmZmZ dd ZdejkZG dd deZdS )    N   )	JSONMixin)Layer   )deck_to_html)settings)View)	ViewState)BaseMapProvider)DARKget_from_map_identifierc                  C   s4   zddl m}  |   W dS  tk
r.   Y dS X d S )Nr   DeckGLWidgetTF)widgetr   ImportErrorr    r   8/tmp/pip-unpacked-wheel-srr0x9ui/pydeck/bindings/deck.pyhas_jupyter_extra   s    r   zgoogle.colabc                   @   s   e Zd Zdedddgededddddd	dddejjdfd
dZ	e
dd ZdedddZdd Zdd ZdddZdd ZdS )DeckNZMapViewT)type
controllerr   r   )ZlatitudeZ	longitudeZzoom100%  c                 C   s   g | _ t|tr| j | n
|p$g | _ || _|| _|p:i }|	| _|
| _|rXt|	 nd| _
|| _t rddlm} | | _tj| j_tj| j_|| j_|| j_|| j_|| j_
| | d}t|tr|tjjkst||| _nt||| _|| _dS )a  This is the renderer and configuration for a deck.gl visualization, similar to the
        `Deck <https://deck.gl/docs/api-reference/core/deck>`_ class from deck.gl.
        Pass `Deck` a Mapbox API token to display a basemap; see the notes below.

        Parameters
        ----------

        layers : pydeck.Layer or list of pydeck.Layer, default None
            List of :class:`pydeck.bindings.layer.Layer` layers to render.
        views : list of pydeck.View, default ``[pydeck.View(type="MapView", controller=True)]``
            List of :class:`pydeck.bindings.view.View` objects to render.
        api_keys : dict, default None
            Dictionary of geospatial API service providers, where the keys are ``mapbox``, ``google_maps``, or ``carto``
            and the values are the API key. Defaults to None if not set. Any of the environment variables
            ``MAPBOX_API_KEY``, ``GOOGLE_MAPS_API_KEY``, and ``CARTO_API_KEY`` can be set instead of hardcoding the key here.
        map_provider : str, default 'carto'
            If multiple API keys are set (e.g., both Mapbox and Google Maps), inform pydeck which basemap provider to prefer.
            Values can be ``carto``, ``mapbox`` or ``google_maps``
        map_style : str or dict, default 'dark'
            One of 'light', 'dark', 'road', 'satellite', 'dark_no_labels', and 'light_no_labels', a URI for a basemap
            style, which varies by provider, or a dict that follows the Mapbox style `specification <https://docs.mapbox.com/mapbox-gl-js/style-spec/>`.
            The default is Carto's Dark Matter map. For Mapbox examples, see  Mapbox's `gallery <https://www.mapbox.com/gallery/>`.
            If not using a basemap, set ``map_provider=None``.
        initial_view_state : pydeck.ViewState, default ``pydeck.ViewState(latitude=0, longitude=0, zoom=1)``
            Initial camera angle relative to the map, defaults to a fully zoomed out 0, 0-centered map
            To compute a viewport from data, see :func:`pydeck.data_utils.viewport_helpers.compute_view`
        height : int, default 500
            Height of Jupyter notebook cell, in pixels.
        width : int` or string, default '100%'
            Width of visualization, in pixels (if a number) or as a CSS value string.
        tooltip : bool or dict of {str: str}, default True
            If ``True``/``False``, toggles a default tooltip on visualization hover.
            Layers must have ``pickable=True`` set in order to display a tooltip.
            For more advanced usage, the user can pass a dict to configure more custom tooltip features.
            Further documentation is `here <tooltip.html>`_.

        .. _Deck:
            https://deck.gl/docs/api-reference/core/deck
        .. _gallery:
            https://www.mapbox.com/gallery/
        Nr   r   zQThe map_provider parameter must be 'mapbox' when map_style is provided as a dict.)layers
isinstancer   appendviewsinitial_view_statedescriptioneffectsstrlowermap_provider_tooltipr   r   r   deck_widgetpydeck_settingscustom_librariesconfigurationheightwidthtooltip_set_api_keysdictr
   ZMAPBOXvalueAssertionError	map_styler   
parameters)selfr   r   r/   api_keysr   r)   r(   r*   r   r   r"   r0   r   Zcustom_map_style_errorr   r   r   __init__   s6    8





zDeck.__init__c                 C   s   | j jsd S | j jS )N)r$   selected_datar1   r   r   r   r4   |   s    zDeck.selected_data)r2   c                 C   sr   |D ]}|ot | qt D ]R}|j d}|j d}||jpJt|}t| || t rt| j|| qdS )zQSets API key for base map provider for both HTML embedding and the Jupyter widget_keyZ_API_KEYN)	r
   r-   namegetosgetenvsetattrr   r$   )r1   r2   kZprovider	attr_nameZprovider_env_var
attr_valuer   r   r   r+      s    zDeck._set_api_keysc                 C   s$   t r| jdd n|   | jS dS )z2Display current Deck object for a Jupyter notebookTnotebook_displayN)in_google_colabto_htmlupdater$   r5   r   r   r   show   s    z	Deck.showc                 C   sV   t  std|  | j_d}g }| jD ]}|jr(||  d}q(|rR|| j_	dS )a  Update a deck.gl map to reflect the current configuration

        For example, if you've modified data passed to Layer and rendered the map using `.show()`,
        you can call `update` to change the data on the map.

        Intended for use in a Jupyter environment.
        zbInstall the Jupyter extra for pydeck with your package manager, e.g. `pip install pydeck[jupyter]`FTN)
r   r   to_jsonr$   Z
json_inputr   Zuse_binary_transportextendZget_binary_dataZdata_buffer)r1   Z
has_binaryZbinary_data_setslayerr   r   r   rC      s    
zDeck.updateFc                 K   s@   |   }	t|	f| j| j|||||| jtjtj||d|}
|
S )a  Write a file and loads it to an iframe, if in a Jupyter environment;
        otherwise, write a file and optionally open it in a web browser

        Parameters
        ----------
        filename : str, default None
            Name of the file.
        open_browser : bool, default False
            Whether a browser window will open or not after write.
        notebook_display : bool, default None
            Display the HTML output in an iframe if True. Set to True automatically if rendering in Jupyter.
        iframe_width : str or int, default '100%'
            Width of Jupyter notebook iframe in pixels, if rendered in a Jupyter environment.
        iframe_height : int, default 500
            Height of Jupyter notebook iframe in pixels, if rendered in Jupyter or Colab.
        as_string : bool, default False
            Returns HTML as a string, if True and ``filename`` is None.
        css_background_color : str, default None
            Background color for visualization, specified as a string in any format accepted for CSS colors.

        Returns
        -------
        str
            Returns absolute path of the file
        )
mapbox_keygoogle_maps_keyfilenameopen_browserr@   iframe_heightiframe_widthr*   r&   r'   	as_stringoffline)rE   r   rH   rI   r#   r%   r&   r'   )r1   rJ   rK   r@   rM   rL   rN   rO   kwargsZ	deck_jsonfr   r   r   rB      s(    $zDeck.to_htmlc                 C   s   | j dd}t|ddS )NTr?   data )rB   getattr)r1   htmlr   r   r   _repr_html_   s    zDeck._repr_html_)N)NFNr   r   FF)__name__
__module____qualname__r   r   r	   r
   ZCARTOr-   r3   propertyr4   r,   r+   rD   rC   rB   rV   r   r   r   r   r      s6   
`
       
7r   )r9   sysZ
json_toolsr   rG   r   Zio.htmlr   r   r%   viewr   Z
view_stater	   Zbase_map_providerr
   Z
map_stylesr   r   r   modulesrA   r   r   r   r   r   <module>   s   

