o
    hf                  	   @  s   d 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	gZd
dejiiZd
dejiiZdd Z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#d	ZdS )&z7Magic functions for rendering vega-lite specifications.    )annotationsN)	find_spec)Any)magic_arguments)is_pandas_dataframe)v5vegalite	vega-lite5c                 C  sf   | du s	t | tr| S t| r|  }r|| } | S t | tr$d| iS tjdt|  ddd | S )z1Convert input data to data for use within schema.Nurlzdata of type z not recognized   )
stacklevel)
isinstancedictr   getstrwarningswarntype)datadata_transformersfunc r   B/var/www/vscode/kcb/lib/python3.10/site-packages/altair/_magics.py_prepare_data   s   
r   namer   returnr   c                 C  sJ   ddl m} |  }r| |jvrd|  d}t||j|  S d}t|)z+Get a variable from the notebook namespace.r   )get_ipythonz
argument 'z1' does not match the name of any defined variablez[Magic command must be run within an IPython environment, in which get_ipython() is defined.)IPython.core.getipythonr   user_ns	NameError
ValueError)r   r   ipmsgr   r   r   _get_variable.   s   


r$   r   ?zBlocal variablename of a pandas DataFrame to be used as the dataset)nargshelpz-vz	--versionversionr   )destdefaultz-jz--jsonjson
store_true)r)   actionvegalite_v5.VegaLitec              
   C  s   t t| }ddi}||j }|td v sJ td | }td | }|jr,t|}n,tdsLzt|}W n  tj	yK } zd}	t
|	|d}~ww ddl}
|
j||
jd}|jdurit|j}t|||d	< ||S )
aM  
    Cell magic for displaying vega-lite visualizations in CoLab.

    %%vegalite [dataframe] [--json] [--version='v5']

    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   r
   r	   yamlzH%%vegalite: spec is not valid JSON. Install pyyaml to parse spec as yamlNr   )Loaderr   )r   parse_argstringr   r(   	RENDERERSTRANSFORMERSr+   loadsr   JSONDecodeErrorr!   r/   load
SafeLoaderr   r$   r   )linecellargsexisting_versionsr(   VegaLiter   specerrr#   r/   r   r   r   r   r   ?   s.   



)r   r   r   r   )r   r.   )__doc__
__future__r   r+   r   importlib.utilr   typingr   IPython.corer   narwhals.stable.v1.dependenciesr   altair.vegaliter   vegalite_v5__all__r<   r2   r   r3   r   r$   argumentr   r   r   r   r   <module>   s:    
