
    g%                    |   U d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ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mZmZ d dlmZ er2d dlZd dl m!Z! d dl"m#Z# d dl$m%Z% ejL                  dk\  rd dlm'Z' nd dl(m'Z'  e       Z)de*d<   dZ+de*d<   	  e ed            Z,de*d<   e,r ed      e,k  rd(dZ.nd(dZ. G d de      Z/ee/ef   Z0e	 d)	 	 	 	 	 d*d       Z1e	 d+	 	 	 	 	 d,d       Z1e	 d+	 	 	 	 	 d-d        Z1	 d.	 	 	 	 	 d/d!Z1d0d"Z2d1d#Z3	 	 	 	 	 	 d2d$Z4d3d%Z5d4d&Z6d5d'Z7y# e-$ r dZ,Y w xY w)6    )annotationsN)version)TYPE_CHECKINGAnyCallableFinal	TypedDictUnionoverload)WeakValueDictionary)is_into_dataframe)Version)import_vegafusion)DataFrameLike)DataTypeMaxRowsErrorSupportsGeoInterfaceToValuesReturnType)default_data_transformer)MutableMapping)IntoDataFrame)
ChartState)      )TypeIsz"MutableMapping[str, DataFrameLike]extracted_inline_tableszvegafusion+dataset://r   VEGAFUSION_PREFIX
vegafusionzVersion | NoneVEGAFUSION_VERSIONz2.0.0a0c                <    t        | t              xs t        |       S N)
isinstancer   r   datas    R/var/www/openai/venv/lib/python3.12/site-packages/altair/utils/_vegafusion_data.pyis_supported_by_vfr&   6   s     $.I2CD2II    c                "    t        | t              S r!   )r"   r   r#   s    r%   r&   r&   =   s    $..r'   c                      e Zd ZU ded<   y)_ToVegaFusionReturnUrlDictstrurlN)__name__
__module____qualname____annotations__ r'   r%   r*   r*   A   s    	Hr'   r*   c                     y r!   r1   r$   max_rowss     r%   vegafusion_data_transformerr5   H        r'   c                     y r!   r1   r3   s     r%   r5   r5   N   r6   r'   c                     y r!   r1   r3   s     r%   r5   r5   T   s      r'   c                    | t         S t        |       rKt        | t              s;dt	        j
                          j                  dd      }| t        |<   dt        |z   iS t        |       S )zVegaFusion Data Transformer.table_-_r,   )
r5   r&   r"   r   uuiduuid4replacer   r   r   )r$   r4   
table_names      r%   r5   r5   Z   si     |**$
49M(Ndjjl^,44S#>
.2
+(:566
 (--r'   c                @   t               }| j                  dg       D ]M  }|j                  dd      }|j                  t              s+|t	        t              d }|j                  |       O | j                  dg       D ]  }|j                  t        |              |S )a  
    Get a set of the inline datasets names in the provided Vega spec.

    Inline datasets are encoded as URLs that start with the table://
    prefix.

    Parameters
    ----------
    vega_spec: dict
        A Vega specification dict

    Returns
    -------
    set of str
        Set of the names of the inline datasets that are referenced
        in the specification.

    Examples
    --------
    >>> spec = {
    ...     "data": [
    ...         {"name": "foo", "url": "https://path/to/file.csv"},
    ...         {"name": "bar", "url": "vegafusion+dataset://inline_dataset_123"},
    ...     ]
    ... }
    >>> get_inline_table_names(spec)
    {'inline_dataset_123'}
    r$   r,    Nmarks)setget
startswithr   lenaddupdateget_inline_table_names)	vega_spectable_namesr$   r,   namemarks         r%   rJ   rJ   l   s    : %K fb)hhub!>>+,s,-/0DOOD!	 * gr*1$78 + r'   c                    t        |       }|j                  t              }|D ci c]  }|t        j                  |       c}S c c}w )a&  
    Get the inline tables referenced by a Vega specification.

    Note: This function should only be called on a Vega spec that corresponds
    to a chart that was processed by the vegafusion_data_transformer.
    Furthermore, this function may only be called once per spec because
    the returned dataframes are deleted from internal storage.

    Parameters
    ----------
    vega_spec: dict
        A Vega specification dict

    Returns
    -------
    dict from str to dataframe
        dict from inline dataset name to dataframe object
    )rJ   intersectionr   pop)rK   inline_namesrL   ks       r%   get_inline_tablesrT      sJ    & *)4L ++,CDK7BC{!A&**1--{CCCs   Ac                8   ddl m}m} t               }|j	                         }|d}t        |       ||       }t        |      }|j                  j	                  dd      }	|j                  j                  ||||	      }
t        |	|
j                                |
S )ax  
    Compile a Vega-Lite spec to a VegaFusion ChartState.

    Note: This function should only be called on a Vega-Lite spec
    that was generated with the "vegafusion" data transformer enabled.
    In particular, this spec may contain references to extract datasets
    using table:// prefixed URLs.

    Parameters
    ----------
    vegalite_spec: dict
        A Vega-Lite spec that was generated from an Altair chart with
        the "vegafusion" data transformer enabled
    local_tz: str
        Local timezone name (e.g. 'America/New_York')

    Returns
    -------
    ChartState
        A VegaFusion ChartState object
    r   data_transformersvegalite_compilersN)No active vega-lite compiler plugin foundr4   )local_tzinline_datasets	row_limit)altairrW   rX   r   rE   
ValueErrorrT   optionsruntimenew_chart_statehandle_row_limit_exceededget_warnings)vegalite_specrZ   rW   rX   vfcompilermsgrK   inline_tablesr\   chart_states              r%   !compile_to_vegafusion_chart_staterj      s    2 =		B "%%'H9o'I &i0M "))--j$?I**,,%	 - K i)A)A)CDr'   c                >   ddl m}m} t               }|j	                         }|d}t        |       ||       }t        |      }|j                  j	                  dd      }|j                  j                  ||j                         ||      \  }	}
t        ||
       |	S )aR  
    Compile a Vega-Lite spec to Vega and pre-transform with VegaFusion.

    Note: This function should only be called on a Vega-Lite spec
    that was generated with the "vegafusion" data transformer enabled.
    In particular, this spec may contain references to extract datasets
    using table:// prefixed URLs.

    Parameters
    ----------
    vegalite_spec: dict
        A Vega-Lite spec that was generated from an Altair chart with
        the "vegafusion" data transformer enabled

    Returns
    -------
    dict
        A Vega spec that has been pre-transformed by VegaFusion
    r   rV   NrY   r4   )r[   r\   )r]   rW   rX   r   rE   r^   rT   r_   r`   pre_transform_specget_local_tzrb   )rd   rW   rX   re   rf   rg   rK   rh   r\   transformed_vega_specwarningss              r%   compile_with_vegafusionrp      s    * =		B "%%'H9o'I &i0M "))--j$?I&(jj&C&C
%	 'D '#8 i2  r'   c                \    |D ]'  }|j                  d      dk(  sd|  d}t        |       y )NtypeRowLimitExceededzXThe number of dataset rows after filtering and aggregation exceeds
the current limit of z. Try adding an aggregation to reduce
the size of the dataset that must be loaded into the browser. Or, disable
the limit by calling alt.data_transformers.disable_max_rows(). Note that
disabling this limit may cause the browser to freeze or crash.)rE   r   )r\   ro   warningrg   s       r%   rb   rb     sF    ;;v"44((1{ 3QQ  s## r'   c                 ,    ddl m}  | j                  dk(  S )z9Check whether the vegafusion data transformer is enabled.r   rW   r   )r]   rW   activerv   s    r%   using_vegafusionrx   +  s     )##|33r'   )r$   r   returnzTypeIs[DataFrameLike])..)r$   Noner4   intry   zCallable[..., Any]).)r$   r   r4   r{   ry   r   )r$   z+dict | IntoDataFrame | SupportsGeoInterfacer4   r{   ry   _VegaFusionReturnType)Ni )r$   zDataType | Noner4   r{   ry   z*Callable[..., Any] | _VegaFusionReturnType)rK   dict[str, Any]ry   zset[str])rK   r}   ry   zdict[str, DataFrameLike])rd   r}   rZ   r+   ry   r   )rd   r}   ry   r}   )r\   r{   ro   list)ry   bool)8
__future__r   r=   importlib.metadatar   importlib_versiontypingr   r   r   r   r	   r
   r   weakrefr   narwhals.stable.v1.dependenciesr   packaging.versionr   altair.utils._importersr   altair.utils.corer   altair.utils.datar   r   r   r   altair.vegalite.datar   syscollections.abcr   narwhals.stable.v1.typingr   vegafusion.runtimer   version_infor   typing_extensionsr   r0   r   r   ImportErrorr&   r*   r|   r5   rJ   rT   rj   rp   rb   rx   r1   r'   r%   <module>r      s   "  ; R R R ' = % 5 +  :.7-
7"!, ?R>S ; S
 3 5 2)01B<1P)QQ
 '),0BBJ/  8:LLM  
&)
 # 

 
),
#& 

 
GJ 
5 AD   
  39.
.,/./.$*ZD45!5-055p0!f
$4w  s   D1 1D;:D;