a
    DfV                     @  s  d dl mZ d dlZ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
mZmZmZ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mZmZ d dlmZ d d	lmZ d d
lmZm Z  d dl!m"Z" d dl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z:m;Z;m<Z<m=Z= ddl>m?Z?m>Z> e:@dZAe:@dZBe:@dZCe:@dZDeEeFjGjGZHe)ej&ZIe)e&ZJdZKdZLe6d de&Mdd d  ZNe6d d!eI d  ZOe4 d"eJ d ZPe5 d#eI d ZQd$eK d%ZRd$eK d&ZSd'eL d(ZTd)e4 d*ZUd+eL d,ZVd-eR d.ZWd-eS d.ZXi ZYe6d/ ZZeZd0 eZd1 eZd2 eZd3 eZd4 eZd5 eZd6 gZ[ed7 Z\d8Z]d9Z^d:Z_d;Z`d<ZaejbG d=d> d>ZcG d?d@ d@eZddfdBdCZedgdDdEZfdhdIdJdJdKdLdMdNdOdMdMdMdPdQdRdSZgdidUdVdWdLdMdOdNdMdMdMdMdXdYdZZhdjd\dOd]dWd^d_d`ZidddTdFdAdAdAi d[dFdAdHdHdAfdadVdOdLdWdMdMdMdbd]dNdMdMdMdMdcdddeZjdS )k    )annotationsN)IOAnyDictListLiteral)SessionContext)CodeHandler)serialize_json)FILEMACROSget_env)Document)script_for_render_items)
RenderItem%standalone_docs_json_and_render_items)wrap_in_script_tag)make_id   )__version__config)base_versionescape   )Application build_single_handler_application)LOADING_INDICATOR_CSS_CLASS)find_requirements)
BASE_TEMPLATECDN_DISTCDN_ROOTDIST_DIRINDEX_TEMPLATE	Resources_envbundle_resourcesloading_cssset_resource_mode)
set_curdocstatesite.webmanifestserviceWorker.jszpyodide_worker.jszpyodide_handler.jszv0.25.0z2024.2.1wheelszpanel-z-dirty z-py3-none-any.whlzbokeh-zwheels/panel-zwheels/bokeh-z!https://cdn.jsdelivr.net/pyodide/z/full/pyodide.jsz/pyc/pyodide.jsz;<link rel="stylesheet" href="https://pyscript.net/releases/z/core.css" />z<link rel="stylsheet" href="zcss/pyscript.css" />z9<script type="module" src="https://pyscript.net/releases/z/core.js"></script>z<script src="z"></script>imageszfavicon.icozicon-vector.svgzicon-32x32.pngzicon-192x192.pngzicon-512x512.pngzapple-touch-icon.pngzindex_background.png)pyodidepyscriptpyodide-workerpyscript-workerzN
import asyncio

from panel.io.pyodide import init_doc, write_doc

init_doc()
z
await write_doc()z$
asyncio.ensure_future(write_doc());a@  
<script type="text/javascript">
async function main() {
  let pyodide = await loadPyodide();
  await pyodide.loadPackage("micropip");
  await pyodide.runPythonAsync(`
    import micropip
    await micropip.install([{{ env_spec }}]);
  `);
  code = `{{ code }}`
  await pyodide.runPythonAsync(code);
}
main();
</script>
a  
<script type="text/javascript">
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('./serviceWorker.js').then(reg => {
    reg.onupdatefound = () => {
      const installingWorker = reg.installing;
      installingWorker.onstatechange = () => {
        if (installingWorker.state === 'installed' &&
            navigator.serviceWorker.controller) {
          // Reload page if service worker is replaced
          location.reload();
        }
      }
    }
  })
}
</script>
c                   @  s&   e Zd ZU ded< ded< ded< dS )Requestdictheaderscookies	argumentsN)__name__
__module____qualname____annotations__ r<   r<   ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/convert.pyr3   {   s   
r3   c                      sH   e Zd Zdd fdd
Zdd Zeddd	d
Zedd Z  ZS )MockSessionContextNdocumentc                  s"   || _ t j|d d d| d S )N)Zserver_contextZ
session_id)Z	_documentsuper__init__)selfr@   argskwargs	__class__r<   r=   rB      s    zMockSessionContext.__init__c                 G  s   d S Nr<   )rC   rD   r<   r<   r=   with_locked_document   s    z'MockSessionContext.with_locked_documentbool)returnc                 C  s   dS )NFr<   rC   r<   r<   r=   	destroyed   s    zMockSessionContext.destroyedc                 C  s   t i i i dS )N)r5   r6   r7   )r3   rL   r<   r<   r=   request   s    zMockSessionContext.request)	r8   r9   r:   rB   rI   propertyrM   rN   __classcell__r<   r<   rF   r=   r>      s   r>   Tc                 C  sJ   |rd}d}d}nd  } }}dd t |  D }tj|||||tdS )Nr*   zimages/favicon.icozimages/apple-touch-icon.pngc                 S  s"   i | ]\}}|d t j| qS )./)ospathbasename).0labelfr<   r<   r=   
<dictcomp>       zmake_index.<locals>.<dictcomp>)itemsmanifest
apple_iconfavicontitleZ	PANEL_CDN)sortedrZ   r"   renderr   )filesr^   r[   r]   r\   rZ   r<   r<   r=   
make_index   s    rb   c                 K  sH   t | dkr|pd}d}n|p d}t|  d }tjf ||d|S )Nr   zPanel Applications
index.htmlr   )namerS   )lenlistvaluesPWA_MANIFEST_TEMPLATEr`   )ra   r^   rE   rS   r<   r<   r=   build_pwa_manifest   s    ri   autor/   Fzstr | os.PathLike | IOzLiteral['auto'] | List[str]z"Literal['auto'] | List[str] | NoneRuntimesrJ   zLiteral['auto', 'local'] | strz
str | Nonestr)filenamerequirementsjs_resourcescss_resourcesruntime	prerenderpanel_versionr[   
http_patchinlinecompiledrK   c           -        s  t | dr6t|  dd}dtt  }t|}n4t| }d	|j
ddd }tt| }t fdd	_t$ | td W d   n1 s0    Y  |jd
 jj}jstd|  d|dkrt|}nt|tr~t| r~t|jdd }z$d
dlm   fdd|D }W n6 t y| } zt!d| |W Y d}~n
d}~0 0 |dkrdtt"# dd  }dtt$# dd  }n(|dkrt%}t&}nd| }dt' }||g}|r|(d |dd |D  }t)* D ]6\t+fdd|D rfdd|D }q|dkr^t,nt-}d	t.||g}d}|du rg }|/dr|dkrt0g}|dkrt1t2g}n
|sg }t34|d gd!}d"|v rd#| d$| d%}|}nd&| d'| d(}n|dkrg }d)	d*d |D }|5d+6d7d,d-}|d.kr|dkrXg }t89|t:j;d/}t<9|
rxt=nt>||d0}t?|}n4|dkr|
rt@ntAg}tBCtD}|9||d1}|rtE }tF\}}|d
 } tGtH|d2d3}!|t?|!d4|7 }|t?tI||7 }ntJd5jd2d6} | g}jK}"|"du rDtL}"nt|"tr`tM Cd7|" }"tN|	rld8nd9d:}#|"tLtOfv r|	rtPd; d< jdd}$d=|$ d>}%nd?tQ d@}%|(|% tRt:j;t:jSt:jT}&|(d=|& dA t  tUj|#\}'}(W d   n1 s0    Y  |r,tV|'gn|'g})d	||) }'d	|(g| }(jW}*|*X }+|+YtZj[|'|(||tLt\| | j|tQdB |"9|+},|,7dCdDt] dEt:j; dF},|dGkr|,7dHdI7dJdK7dLdM},|,|fS )Na7  
    Converts a Panel or Bokeh script to a standalone WASM Python
    application.

    Arguments
    ---------
    filename: str | Path | IO
        The filename of the Panel/Bokeh application to convert.
    requirements: 'auto' | List[str]
        The list of requirements to include (in addition to Panel).
    js_resources: 'auto' | List[str]
        The list of JS resources to include in the exported HTML.
    css_resources: 'auto' | List[str] | None
        The list of CSS resources to include in the exported HTML.
    runtime: 'pyodide' | 'pyscript'
        The runtime to use for running Python in the browser.
    prerender: bool
        Whether to pre-render the components so the page loads.
    panel_version: 'auto' | str
        The panel release version to use in the exported HTML.
    http_patch: bool
        Whether to patch the HTTP request stack with the pyodide-http library
        to allow urllib3 and requests to work.
    inline: bool
        Whether to inline resources.
    compiled: bool
        Whether to use pre-compiled pyodide bundles.
    readz
convert.py)sourcerm   zapp-.Nc                     s
   t  dS )Nr?   )r>   r<   r?   r<   r=   <lambda>   rY   z script_to_html.<locals>.<lambda>r   z	The file z~ does not publish any Panel contents. Ensure you have marked items as servable or added models to the bokeh document manually.rj   utf-8encoding)Requirementc                   s.   g | ]&}| d d   r rqS )#r   )splitstrip)rU   r)r   r2r<   r=   
<listcomp>   s   z"script_to_html.<locals>.<listcomp>z,Requirements parser raised following error: localrQ   /zpanel==zbokeh==zpyodide-http==0.2.1c                 S  s   g | ]}|d vr|qS ))Zpanelbokehr<   rU   reqr<   r<   r=   r     s   c                 3  s   | ]} |v V  qd S rH   r<   r   )rd   r<   r=   	<genexpr>  rY   z!script_to_html.<locals>.<genexpr>c                   s&   g | ]}|v r d   n|qS )z>=r<   r   )min_versionrd   r<   r=   r     rY   r0   
z!error)packagesZpluginsworkerz'<script type="py" async worker config='z' src="z.py"></script>z<py-script config='z'>z</py-script>, c                 S  s   g | ]}t |qS r<   reprr   r<   r<   r=   r   (  rY   unicode_escape`z\`r1   )rd   loading_spinner)PYODIDE_URLenv_speccode)r   r   F)quotezapplication/jsonr-   )tokenrootsZuse_for_titlez{% extends base %}
ru   cdn)modecsszloading.cssz<style type="text/css">
z	
</style>z<link rel="stylesheet" href="z#css/loading.css" type="text/css" />z%
.py-error { display: none; }</style>)r^   bokeh_js	bokeh_cssplot_scriptZdocsbasemacrosdocr   r[   Zdist_urlz<body>z<body class="z pn-z">r2   z<script type="text/javascript"z6<script type="text/javascript" crossorigin="anonymous"z<link rel="stylesheet"z.<link rel="stylesheet" crossorigin="anonymous"z<link rel="icon"z(<link rel="icon" crossorigin="anonymous")^hasattrr	   rw   rl   uuiduuid4r   pathlibPathjoinrd   r   r   absoluter   Z_session_contextr(   Zinitialize_documentr)   Z_on_load	_handlersZ_runnerrx   r   RuntimeErrorr   
isinstanceis_file	read_text
splitlinesZpackaging.requirementsr   	Exception
ValueErrorPANEL_LOCAL_WHLas_posixBOKEH_LOCAL_WHLPANEL_CDN_WHLBOKEH_CDN_WHLBOKEH_VERSIONappendMINIMUM_VERSIONSrZ   anyPOST_PYSCRIPTPOSTPRE
startswithPYSCRIPT_JSPYSCRIPT_CSSPYSCRIPT_CSS_OVERRIDESjsondumpsencodedecodereplaceWORKER_HANDLER_TEMPLATEr`   r   r   WEB_WORKER_TEMPLATEPYODIDE_PYC_URLr   r   PYODIDE_PYC_JS
PYODIDE_JS_pn_envZfrom_stringPYODIDE_SCRIPTr   r   r   r
   r   r   templater   r   r#   r   r!   r   r&   Zloading_colorZloading_max_heightr%   INIT_SERVICE_WORKERZ_template_variablescopyupdater4   r^   r   r   )-rm   rn   ro   rp   rq   rr   rs   r[   rt   ru   rv   handlerZapp_nameapprS   rx   eZ	panel_reqZ	bokeh_reqZ	base_reqsreqsZ	post_coder   Z
web_workerZpyconfigr   r   Zworker_handlerscript_templateZjson_idZ	docs_jsonZrender_itemsZrender_itemZescaped_jsonr   	resourcesZloading_baseZloading_styleZspinner_cssr   r   Zextra_jsZtemplate_variablescontexthtmlr<   )r   r@   r   rd   r   r=   script_to_html   s4   *




(






















0

r   r1   zstr | os.PathLikezstr | os.PathLike | Nonez)List[str] | Literal['auto'] | os.PathLike)r   	dest_pathrn   rq   rr   r[   rs   rt   ru   rv   verbosec                 C  s  |d u rt d}nt|t js*t |}zRt|r6dnd. t| ||||||||	d	\}}W d    n1 sp0    Y  W nR ty   Y d S  ty } z(td|  d| d|  W Y d }~d S d }~0 0 d	t
j| dd d	 }| d
}t|| ddd}|| W d    n1 s.0    Y  |dkrt|| d ddd}|| W d    n1 s|0    Y  nN|dkrt|| d ddd}|| W d    n1 s0    Y  |
rtd|  d| d| d |dd|fS )NrQ   ru   r   )rn   rq   rr   r[   rs   rt   ru   rv   zFailed to convert z to z	 target: ry   rz   z.htmlwr|   r}   r2   z.pyr1   z.jszSuccessfully converted z target and wrote output to _ )r   r   r   PurePathr'   r   KeyboardInterruptr   printr   rR   rS   rT   r   openwriter   )r   r   rn   rq   rr   r[   rs   rt   ru   rv   r   r   r   r   rd   rm   outr<   r<   r=   convert_app  s<    
, 
*
,
*r      z	List[str]int)appsr   max_workersrn   c              	     s   dd l }ddlm} i } fddtdt D }|D ]}	||dd}
g }|	D ]D}t|trx||d}n|}|
j	t
||fd|i|}|| q\tj|D ]$}| }|d ur|\}}|||< qW d    q<1 s0    Y  q<|S )	Nr   )ProcessPoolExecutorc                   s   g | ]} ||  qS r<   r<   )rU   ir   r   r<   r=   r     rY   z)_convert_process_pool.<locals>.<listcomp>spawn)r   Z
mp_contextrj   rn   )multiprocessingconcurrent.futuresr   rangere   Zget_contextr   r4   getZsubmitr   r   
concurrentfuturesZas_completedresult)r   r   r   rn   rE   mpr   ra   groupsgroupexecutorr   r   Zapp_requiresrW   futurer   rd   rm   r<   r   r=   _convert_process_pool  s8     

*r   z+str | os.PathLike | List[str | os.PathLike]zDict[Any, Any])r   r   r^   rq   rn   rr   build_index	build_pwa
pwa_configr   rs   rt   ru   rv   r   c              
     s  t | tr| g} du r$tdnt tjs:tjddd |rPdnd}t |tri }| D ].  fdd| D }|r|d nd	| < qfn|}|||||
||||d
	tj	rtfdd| D }nt
| fd|	i}|rTt|dkrTt|||d}td d}|| W d   n1 s<0    Y  |rTtd |s^dS d }|jdd g }tD ]X}t||j d}||  W d   n1 s0    Y  |d|j  qz|rtd t|fd|i|}td ddd}|| W d   n1 s*0    Y  |rBtd tjt j|pVdddd |D d}td  ddd}|| W d   n1 s0    Y  |rtd! dS )"a  
    Arguments
    ---------
    apps: str | List[str]
        The filename(s) of the Panel/Bokeh application(s) to convert.
    dest_path: str | pathlib.Path
        The directory to write the converted application(s) to.
    title: str | None
        A title for the application(s). Also used to generate unique
        name for the application cache to ensure.
    runtime: 'pyodide' | 'pyscript' | 'pyodide-worker'
        The runtime to use for running Python in the browser.
    requirements: 'auto' | List[str] | os.PathLike | Dict[str, 'auto' | List[str] | os.PathLike]
        The list of requirements to include (in addition to Panel).
        By default automatically infers dependencies from imports
        in the application. May also provide path to a requirements.txt
    prerender: bool
        Whether to pre-render the components so the page loads.
    build_index: bool
        Whether to write an index page (if there are multiple apps).
    build_pwa: bool
        Whether to write files to define a progressive web app (PWA) including
        a manifest and a service worker that caches the application locally
    pwa_config: Dict[Any, Any]
        Configuration for the PWA including (see https://developer.mozilla.org/en-US/docs/Web/Manifest)

          - display: Display options ('fullscreen', 'standalone', 'minimal-ui' 'browser')
          - orientation: Preferred orientation
          - background_color: The background color of the splash screen
          - theme_color: The theme color of the application
    max_workers: int
        The maximum number of parallel workers
    panel_version: 'auto' | 'local'] | str
'       The panel version to include.
    http_patch: bool
        Whether to patch the HTTP request stack with the pyodide-http library
        to allow urllib3 and requests to work.
    inline: bool
        Whether to inline resources.
    compiled: bool
        Whether to use the compiled and faster version of Pyodide.
    NrQ   T)parentsexist_okr*   c                   s*   g | ]"\}}  |tjjd r|qS )r   )endswithr   rR   rS   sep)rU   rd   deps)r   r<   r=   r   %  s   z convert_apps.<locals>.<listcomp>r   rj   )	rn   rq   rr   r[   rs   rt   ru   r   rv   c                 3  s    | ]}t | fi V  qd S rH   )r   )rU   r   )r   rE   r<   r=   r   5  rY   zconvert_apps.<locals>.<genexpr>r   r   )r[   r^   rc   r   zSuccessfully wrote index.html.r.   )r  wbzimages/z$Successfully wrote icons and images.r^   r|   r}   z!Successfully wrote site.manifest.zPanel Pyodide Appr   c                 S  s   g | ]}t |qS r<   r   )rU   pr<   r<   r=   r   [  rY   )r   rd   Z	pre_cacher+   z$Successfully wrote serviceWorker.js.)r   rl   r   r   r   mkdirr4   rZ   r)   Z_is_pyodider   re   rb   r   r   r   
PWA_IMAGESrd   
read_bytesr   ri   SERVICE_WORKER_TEMPLATEr`   r   r   hexr   )r   r   r^   rq   rn   rr   r   r   r   r   rs   rt   ru   rv   r   r[   Zapp_requirementsmatchesra   indexrW   Z	imgs_pathZimg_relimgr   r<   )r   r   rE   r=   convert_apps  sz    ;



*.**r  )NT)N)
rj   rj   rj   r/   Trj   NTFT)
Nrj   r1   TNrj   TFFT)Nr   rj   )k
__future__r   r   r   Zdataclassesr   rR   r   r   typingr   r   r   r   r   r   Zbokeh.application.applicationr   Zbokeh.application.handlers.coder	   Zbokeh.core.json_encoderr
   Zbokeh.core.templatesr   r   r   Zbokeh.documentr   Zbokeh.embed.elementsr   Zbokeh.embed.utilr   r   Zbokeh.embed.wrappersr   Zbokeh.util.serializationr   r-   r   r   utilr   r   Zapplicationr   r   Zloadingr   Zmime_renderr   r   r   r   r    r!   r"   r#   r$   r   r%   r&   r'   r)   r(   Zget_templaterh   r  r   r   r   __file__parentZ
PANEL_ROOTr   Z
PY_VERSIONZPYODIDE_VERSIONZPYSCRIPT_VERSIONr   r   r   r   r   r   r   r   r   r   r   r   r   ZICON_DIRr	  rk   r   r   r   r   r   Z	dataclassr3   r>   rb   ri   r   r   r   r  r<   r<   r<   r=   <module>   s   0







          & `          $1   $