U
    ꥡcK$                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ 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mZmZmZ d d
lmZ d dlmZ dZedZedZedZ dddZ!dd Z"G dd deZ#dd Z$dd Z%dS )    N)
itemgetter)nodes)ViewList)	Directive)flag)nested_parse_with_titles   )get_docstring_and_restprev_this_nextcreate_thumbnailcreate_generic_image)
eval_block)iter_exampleszaltair.examplesa  
.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _{{ gallery_ref }}:

{{ title }}
{% for char in title %}-{% endfor %}

This gallery contains a selection of examples of the plots Altair can create.

Some may seem fairly complicated at first glance, but they are built by combining a simple set of declarative building blocks.

Many draw upon sample datasets compiled by the `Vega <https://vega.github.io/vega/>`_ project. To access them yourself, install `vega_datasets <https://github.com/altair-viz/vega_datasets>`_.

.. code-block:: none

   $ pip install vega_datasets

{% for grouper, group in examples %}

.. _gallery-category-{{ grouper }}:

{{ grouper }}
{% for char in grouper %}~{% endfor %}

.. raw:: html

   <span class="gallery">
   {% for example in group %}
   <a class="imagegroup" href="{{ example.name }}.html">
     <span class="image" alt="{{ example.title }}" style="background-image: url({{ image_dir }}/{{ example.name }}-thumb.png);"></span>
     <span class="image-title">{{ example.title }}</span>
   </a>
   {% endfor %}
   </span>

   <div style='clear:both;'></div>

.. toctree::
  :hidden:
{% for example in group %}
  {{ example.name }}
{%- endfor %}

{% endfor %}
a1  
.. raw:: html

    <div id="showcase">
      <div class="examples">
      {% for example in examples %}
        <a class="preview" href="{{ gallery_dir }}/{{ example.name }}.html" style="background-image: url({{ image_dir }}/{{ example.name }}-thumb.png)"></a>
      {% endfor %}
      </div>
    </div>
z
.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_{{ name }}:

{{ docstring }}

.. altair-plot::
    {% if code_below %}:code-below:{% endif %}

    {{ code | indent(4) }}

.. toctree::
   :hidden:
Tc              
   C   s  t j|st | t j|d}t j|rPt|}t|}W 5 Q R X ni }| D ]}|d d }t j||}t	|d 
  }	||d|	k}
|
rt j|rtd| n|td| t|d }z|| |	||< W n( tk
r   td t| Y nX t|d	}t|| W 5 Q R X |rX|d
i }t j||d d }t||f| qXt|d	}t|| W 5 Q R X dS )z-Save example pngs and (optionally) thumbnailsz_image_hashes.jsonnamez.pngcode z-> using cached {}z-> saving {}z)Unable to save image: using generic imagewZgalleryParametersz
-thumb.pngN)ospathexistsmakedirsjoinopenjsonloadhashlibmd5encode	hexdigestgetprintformatr   saveImportErrorwarningswarnr   dumpr   )examples	image_dirZmake_thumbnails	hash_filefhashesexamplefilenameZ
image_fileZexample_hashZhashes_matchZchartparamsZ
thumb_file r/   B/tmp/pip-unpacked-wheel-y9_o96ar/altair/sphinxext/altairgallery.pysave_example_pngsr   s<    




r1   c               	   K   sr   t t tdd}|D ]V}t|d \}}}}||  |dkrDd}||| dd || |d q|S )	z0Iterate through Altair examples and extract coder   keyr-   Nzother charts
r   )	docstringtitler   categorylineno)sortedr   r   r	   updatestripsplitr6   )kwdsr'   r,   r5   r7   r   r8   r/   r/   r0   populate_examples   s    

r>   c                   @   s0   e Zd ZdZeedd eeeedZdd Z	dS )AltairMiniGalleryDirectiveFc                 C   s   t tt|  S )N)listmapintr<   xr/   r/   r0   <lambda>       z#AltairMiniGalleryDirective.<lambda>)sizenamesindicesshuffleseedtitleswidthc                    sh  | j dd}dd | j dddD }| j dg }d	| j k}| j d
d}| j dd}| j dd }| jjjj}|j}	|	jj	j
}
t  |rt||k rtddd  D fdd|D  n@|r fdd|D  |rt| t  |r
 d |  tjd|
 ||d}t }|dD ]}||d q.t }| jj|_t| j|| |jS )NrG      c                 S   s   g | ]}|  qS r/   )r;   .0r   r/   r/   r0   
<listcomp>   s     z2AltairMiniGalleryDirective.run.<locals>.<listcomp>rH   r   ,rI   rJ   rK   *   rL   FrM   zValtair-minigallery: if names are specified, the list must be at least as long as size.c                 S   s   i | ]}|d  |qS )r   r/   )rP   r,   r/   r/   r0   
<dictcomp>   s      z2AltairMiniGalleryDirective.run.<locals>.<dictcomp>c                    s   g | ]} | qS r/   r/   rO   )mappingr/   r0   rQ      s     c                    s   g | ]} | qS r/   r/   )rP   i)r'   r/   r0   rQ      s     /_static)r(   gallery_dirr'   rL   rM   r4   z<altair-minigallery>)optionsr   r<   statedocumentsettingsenvappbuilderconfigaltair_gallery_dirr>   len
ValueErrorrandomrK   rJ   MINIGALLERY_TEMPLATErenderr   appendr   Z	paragraphr   children)selfrG   rH   rI   rJ   rK   rL   rM   r]   r^   rX   includeresultlinenoder/   )r'   rU   r0   run   sN    



	
zAltairMiniGalleryDirective.runN)
__name__
__module____qualname__Zhas_contentrB   strr   boolZoption_specrn   r/   r/   r/   r0   r?      s   
r?   c                 C   s  | j jj}tj| j j|}tj| j jd}| j jj}| j jj}t	|dd}tj
|sdt| t|dd d}tg g g g g g g g g g g d}|D ]}||d  | qttj|d	d
"}	|	tj|| d|d W 5 Q R X t|| t|D ]\}
}}|
r djf |
|d< |r6djf ||d< tj||d d }ttj|d
dd}	|	t| W 5 Q R X q d S )NZ_imagesT)gallery_refZ
code_belowc                 S   s   | d S )Nr6   r/   rC   r/   r/   r0   rE     rF   zmain.<locals>.<lambda>r2   )zSimple Chartsz
Bar ChartszLine ChartszArea ChartszCircular PlotszScatter PlotsZ
HistogramsZMapszInteractive ChartszCase StudieszOther Chartsr7   z	index.rstr   rW   )r6   r'   r(   rt   zgallery_{name}Zprev_refZnext_refr   z.rstzutf-8)encoding)r_   r`   ra   r   r   r   srcdiraltair_gallery_refaltair_gallery_titler>   r   r   r9   collectionsOrderedDictrg   r   writeGALLERY_TEMPLATErf   itemsr1   r
   r!   EXAMPLE_TEMPLATE)r^   rX   
target_dirr(   rt   Zgallery_titler'   Zexamples_tocdr*   Zprev_exr,   Znext_exZtarget_filenamer/   r/   r0   main   sV    





r   c                 C   sR   |  dt | d | ddd | ddd | dd	d | d
dt d S )Nzbuilder-initedzaltair-gallery.cssra   Zgalleryr]   rw   zexample-galleryrx   zExample Gallerypyzaltair-minigallery)connectr   Zadd_css_fileZadd_config_valueZadd_directive_to_domainr?   )r^   r/   r/   r0   setup1  s    
r   )T)&r   r   r   rd   ry   operatorr   r$   Zjinja2Zdocutilsr   Zdocutils.statemachiner   Zdocutils.parsers.rstr   Zdocutils.parsers.rst.directivesr   Zsphinx.util.nodesr   utilsr	   r
   r   r   Zaltair.utils.execevalr   Zaltair.examplesr   ZEXAMPLE_MODULETemplater|   re   r~   r1   r>   r?   r   r   r/   r/   r/   r0   <module>   s<   1
/A8