U
    ꥡcS                  	   @   sb  d Z ddg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	m
Z
 ddlmZ ddlmZ dd	lmZ zddlZd
ZW n ek
r   dZY nX dejiejejddZdejiejejddZdd Zdd Ze ejddddejdddddejdddddd d Ze ejdd!d"dejdddd#dejdddddd$d ZdS )%z=
Magic functions for rendering vega/vega-lite specifications
vegavegalite    N)magic_arguments)curried)v3)v4)v5TF5)34)r   	vega-litec                 C   s`   | dkst | tr| S t | tjr2t| | S t | trDd| iS t	d
t|  | S dS )z0Convert input data to data for use within schemaNurlzdata of type {} not recognized)
isinstancedictpdZ	DataFramer   pipegetstrwarningswarnformattype)datadata_transformers r   2/tmp/pip-unpacked-wheel-y9_o96ar/altair/_magics.py_prepare_data,   s    
r   c                 C   s:   t  }|dkrtd| |jkr0td| |j|  S )z+Get a variable from the notebook namespace.Nz[Magic command must be run within an IPython environemnt, in which get_ipython() is defined.z=argument '{}' does not match the name of any defined variable)IPythonZget_ipython
ValueErrorZuser_ns	NameErrorr   )nameipr   r   r   _get_variable9   s    
r"   r   *zClocal variable name of a pandas DataFrame to be used as the dataset)nargshelpz-vz	--versionversion)destdefaultz-jz--jsonjson
store_true)r'   actionc                 C   s  t t| }|j}|td ks"ttd | }td | }dd }ztt||j	}W n" t
k
rx   t
d| Y nX |jrt|}nDtszt|}W q tjk
r   t
dY qX ntj|tjd}|rg |d< |D ]0\}	}
t|
}
t|
|}|	|d< |d | q||S )	a_  Cell magic for displaying Vega visualizations in CoLab.

    %%vega [name1:variable1 name2:variable2 ...] [--json] [--version='5']

    Visualize the contents of the cell using Vega, optionally specifying
    one or more pandas DataFrame objects to be used as the datasets.

    If --json is passed, then input is parsed as json rather than yaml.
    r   c                 S   sT   |  d} t| dkr&| d | d fS t| dkrB| d | d fS td| d S )N:   r      zinvalid identifier: '{}')splitlenr   r   )sr   r   r   namevarb   s    
zvega.<locals>.namevarzCould not parse arguments: '{}'zD%%vega: spec is not valid JSON. Install pyyaml to parse spec as yamlLoaderr   r    )r   parse_argstringr   r&   	RENDERERSAssertionErrorTRANSFORMERSlistmapr   r   r   r)   loadsYAML_AVAILABLEJSONDecodeErroryamlload
FullLoaderr"   r   append)linecellargsr&   Vegar   r2   r   specr    valZpreppedr   r   r   r   I   s8    	
?zBlocal variablename of a pandas DataFrame to be used as the datasetr   c                 C   s   t t| }|j}|td ks"ttd | }td | }|jrLt|}nDt	szt|}W q tj
k
r|   tdY qX ntj|tjd}|jdk	rt|j}t|||d< ||S )aE  Cell magic for displaying vega-lite visualizations in CoLab.

    %%vegalite [dataframe] [--json] [--version=3]

    Visualize the contents of the cell using Vega-Lite, optionally
    specifying a pandas DataFrame object to be used as the dataset.

    if --json is passed, then input is parsed as json rather than yaml.
    r   zH%%vegalite: spec is not valid JSON. Install pyyaml to parse spec as yamlr3   Nr   )r   r5   r   r&   r6   r7   r8   r)   r;   r<   r=   r   r>   r?   r@   r   r"   r   )rB   rC   rD   r&   VegaLiter   rF   r   r   r   r   r      s&    

) __doc____all__r)   r   r   ZIPython.corer   Zpandasr   Ztoolzr   Zaltair.vegaliter   Zvegalite_v3r   Zvegalite_v4Zaltair.vegar   Zvega_v5r>   r<   ImportErrorrE   rI   r6   r   r8   r   r"   argumentr   r   r   r   r   r   <module>   sZ   
	 7