L i20UddlmZddlZddlmZmZmZmZmZddl m Z m Z ddl m Z er ddlZejdk\rddlmZnddlmZed Zd ed <e d dd Ze d dd Ze d ddZe d ddZ d ddZ d ddZ ddZdZddZy) ) annotationsN) TYPE_CHECKINGAnyLiteralcastoverload)import_vl_convertvl_version_for_vl_convert) spec_to_html) ) TypeAlias)htmljsonpngsvgpdfvega vega-literMimeBundleFormatc yN specformatmode vega_versionvegaembed_versionvegalite_version embed_optionsenginekwargss ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/altair/utils/mimebundle.pyspec_to_mimebundler&s!$c yrrrs r%r&r&"r'c yrrrs r%r&r&.r)r'c yrrrs r%r&r&:s-0r'c ddlm} ddlm} m} |dk7r d} t | |} | r | |}d} || j jdi}n|}t|}|d vrCt|ttd || f||jd d|jd dd |S|dk(rt|f| ||||d|}d|iS|dk(r| d} t | d|dd|iS|dk(rd|iSd} t | )aE Convert a vega-lite specification to a mimebundle. The mimebundle type is controlled by the ``format`` argument, which can be one of the following ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'] Parameters ---------- spec : dict a dictionary representing a vega-lite plot spec format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'} the file format to be saved. mode : string {'vega-lite'} The rendering mode. vega_version : string The version of vega.js to use vegaembed_version : string The version of vegaembed.js to use vegalite_version : string The version of vegalite.js to use. Only required if mode=='vega-lite' embed_options : dict (optional) The vegaEmbed options dictionary. Defaults to the embed options set with alt.renderers.set_embed_options(). (See https://github.com/vega/vega-embed for details) engine: string {'vl-convert'} the conversion engine to use for 'png', 'svg', 'pdf', and 'vega' formats **kwargs : Additional arguments will be passed to the generating function Returns ------- output : dict a mime-bundle representing the image Note ---- The png, svg, pdf, and vega outputs require the vl-convert package r) renderers)compile_with_vegafusionusing_vegafusionrzmode must be 'vega-lite'rNr">rrrr)rrrr formatLocaletimeFormatLocale)r# format_localetime_format_localer)rrr r!r"z text/htmlzMust specify vegalite_versionzapplication/vnd.vegalite.vz+jsonrzapplication/jsonzPformat must be one of ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']) altairr-altair.utils.displayr.r/ ValueErroroptionsgetpreprocess_embed_options_spec_to_mimebundle_with_enginerrr )rrrrr r!r"r#r$r-r.r/msg internal_modefinal_embed_optionsrs r%r&r&Fssd!N {(o26M&t, '//33ORH+,-@AM ...  45v >  '++NDA,001CTJ     6   %/-'   T"" ;   #1CS/ !,-=a-@,AGNN 6 "D)) I or'c |jdd}t||}|dk(rWt}t} |dk(r"|dk(r|} d| iS|j || } d| iS|dk(r6|dk(r|j |||} d | iS|j || || } d | iS|d k(r}|jd d } d} |jd| }|dk(r|j|| |||}n|j|| | |||}|| z }t|\}}d|id||z ||z difS|dk(rJ|jd d } |dk(r|j|| ||}d|iS|j|| | ||}d|iSd|}t|d|}t|)a Helper for Vega-Lite to mimebundle conversions that require an engine. Parameters ---------- spec : dict a dictionary representing a vega-lite plot spec format : string {'png', 'svg', 'pdf', 'vega'} the format of the mimebundle to be returned mode : string {'vega-lite', 'vega'} The rendering mode. engine: string {'vl-convert'} the conversion engine to use format_locale : str or dict d3-format locale name or dictionary. Defaults to "en-US" for United States English. See https://github.com/d3/d3-format/tree/main/locale for available names and example definitions. time_format_locale : str or dict d3-time-format locale name or dictionary. Defaults to "en-US" for United States English. See https://github.com/d3/d3-time-format/tree/main/locale for available names and example definitions. **kwargs : Additional arguments will be passed to the conversion function r#N vlconvertr) vl_versionzapplication/vnd.vega.v5+jsonr)r2r3)r@r2r3z image/svg+xmlr scale_factorr Hppi)scalerCr2r3)r@rDrCr2r3z image/png)widthheightr)rDr2r3)r@rDr2r3zapplication/pdfzUnexpected format zUnexpected normalized_engine )pop_validate_normalize_enginer r vegalite_to_vega vega_to_svgvegalite_to_svgr8 vega_to_pngvegalite_to_png_pngxy vega_to_pdfvegalite_to_pdfr6)rrrr2r3r$r#normalized_enginevlcr@vgrrD default_ppirCrfactorwhrr;s r%r:r:sUDZZ$ 'F266BK'!.0 V v~3B7 7))$:)F2B7 7 u_v~oo"/'9&$S) ) )))"/'9 * $S) ) u_JJ~q1EK**UK0Cv~oo"/'9 &)))"/'9 *;&F#;DAq%q6zQZH( u_JJ~q1Ev~oo"/'9 &&s+ +)))"/'9 *&s+ +'vj1CS/ !..?-BCor'c" t}|dn/|jjddjdd}|dk(r| d}t ||S||d}|Sd|d}t |d |d }t |#t$rd}YwxYw) z Helper to validate and normalize the user-provided engine. engine : {None, 'vl-convert'} the user-provided engine string format : string {'png', 'svg', 'pdf', 'vega'} the format of the mimebundle to be returned N-_r?zIThe 'vl-convert' conversion engine requires the vl-convert-python packagezSaving charts in z format requires the vl-convert-python package: see https://altair-viz.github.io/user_guide/saving_charts.html#png-svg-and-pdf-formatzInvalid conversion engine z. Expected vl-convert)r ImportErrorlowerreplacer6)r#rrRrQr;s r%rHrH&s! FLLN$:$:3$C$K$KCQS$T K' ;]CS/ !   " ? +  $F:.hh S/ !*6*4IJo1 s B B Bcb|jd}tjd||dz|dzS)zU read the (width, height) from a PNG header. Taken from IPython.display sIHDRz>ii )indexstructunpack)dataihdrs r%rNrNQs2 ::g D ==TAXr : ;;r'c.|xsij}|jdd}t|trt }|j ||d<|jdd}t|trt }|j ||d<|S)a Preprocess embed options to a form compatible with Vega Embed. Parameters ---------- embed_options : dict The embed options dictionary to preprocess. Returns ------- embed_opts : dict The preprocessed embed options dictionary. r0Nr1)copyr8 isinstancestrr get_format_localeget_time_format_locale)r"r2rRr3s r%r9r9\s#(b..0M"%%nd;M-%!(+(=(=m(L n%&**+=tD$c*!,/,F,F - () r')......)rdict[str, Any]rzLiteral['json', 'vega-lite']rLiteral['vega-lite'] | Noner str | Noner ror!ror"dict[str, Any] | Noner#Literal['vl-convert'] | Nonereturnzdict[str, dict[str, Any]])rrmrzLiteral['html']rrnrror ror!ror"rpr#rqrrzdict[str, str])rrmrzLiteral['pdf', 'svg', 'vega']rrnrror ror!ror"rpr#rqrrrm)rrmrzLiteral['png']rrnrror ror!ror"rpr#rqrrz%tuple[dict[str, Any], dict[str, Any]])NNNNNN)rrmrrrrnrror ror!ror"rpr#rqrrz6dict[str, Any] | tuple[dict[str, Any], dict[str, Any]])NN) rdictr$Literal['png', 'svg', 'pdf', 'vega']rzLiteral['vega-lite', 'vega']r2str | dict | Noner3rurrr)r#rqrrtrrrj)r"rsrrrs) __future__rrctypingrrrrr _importersr r rr sys version_infortyping_extensionsr__annotations__r&r:rHrNr9rr'r%r}s/" >>D 7"$/%<)  ),"$'#&+.+. $  $ ( $ & $ $ " $ ! $) $ ) $ $  $ ),"$'#&+.+.      &   "  ! )  )     ),"$'#&+.+.    )  &   "  ! )  )     ),"$'#&+.+. 0  0  0 & 0 0 " 0 ! 0) 0 ) 0+ 0  0)-#$(#'+/+/g g g &g g " g ! g)g )g<g\(,,0 s s 0s 's% s * s sl( (( 0( (V<r'