
    -e1                       d Z ddlmZ ddlmZ  ej        e          ZddlZddl	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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' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z. dZ/ ed          Z0 e.            Z1 G d dej2        ej3                  Z4 G d de)          Z5d Z6d Z7d Z8d Z9d'd#Z:d(d%Z;d& Z<dS ))a@   Include Bokeh plots in Sphinx HTML documentation.

For other output types, the placeholder text ``[graph]`` will
be generated.

The ``bokeh-plot`` directive can be used by either supplying:

**A path to a source file** as the argument to the directive::

    .. bokeh-plot:: path/to/plot.py

.. note::
    .py scripts are not scanned automatically! In order to include
    certain directories into .py scanning process use following directive
    in sphinx conf.py file: bokeh_plot_pyfile_include_dirs = ["dir1","dir2"]

**Inline code** as the content of the directive::

 .. bokeh-plot::

     from bokeh.plotting import figure, output_file, show

     output_file("example.html")

     x = [1, 2, 3, 4, 5]
     y = [6, 7, 6, 4, 5]

     p = figure(title="example", width=300, height=300)
     p.line(x, y, line_width=2)
     p.circle(x, y, size=10, fill_color="white")

     show(p)

This directive also works in conjunction with Sphinx autodoc, when
used in docstrings.

The ``bokeh-plot`` directive accepts the following options:

process-docstring (bool):
    Whether to display the docstring in a formatted block
    separate from the source.

source-position (enum('above', 'below', 'none')):
    Where to locate the block of formatted source code (if anywhere).

linenos (bool):
    Whether to display line numbers along with the source.

Examples
--------

The inline example code above produces the following output:

.. bokeh-plot::

    from bokeh.plotting import figure, output_file, show

    output_file("example.html")

    x = [1, 2, 3, 4, 5]
    y = [6, 7, 6, 4, 5]

    p = figure(title="example", width=300, height=300)
    p.line(x, y, line_width=2)
    p.circle(x, y, size=10, fill_color="white")

    show(p)

To enable this extension, add `"bokeh.sphinxext.bokeh_plot"` to the extensions
list in your Sphinx configuration module.

    )annotations)loggingN)getenv)basenamedirnamejoin)uuid4)nodes)choiceflag)SphinxError)copyfile	ensuredirstatus_iterator)set_source_info)Document)autoload_static)Model)BokehDeprecationWarning   )PARALLEL_SAFE)BokehDirective)ExampleHandler)	_REPO_TOPget_sphinx_resources)autoload_scriptBokehPlotDirectivesetupGOOGLE_API_KEYc                  6    e Zd Zed             Zej        dfZdS )r   c                    |d         }|d         }|r| j                             d| d           | j                             |           |r| j                             d           t          j        )N
script_tagheight_hintz<div style="height:zpx;">z</div>)bodyappendr
   SkipNode)visitornoder"   r#   s       :lib/python3.11/site-packages/bokeh/sphinxext/bokeh_plot.py
visit_htmlzautoload_script.visit_html   s}    ,'
=) 	JL Hk H H HIIIJ''' 	*L)))n    N)__name__
__module____qualname__staticmethodr*   __func__html r+   r)   r   r      s6          \ $DDDr+   r   c                  H    e Zd ZdZdZd d d dZd ZddZd Zd Z	d Z
dS )r   T   c                $    t          |           d u S Nr   xs    r)   <lambda>zBokehPlotDirective.<lambda>   s    tAww$ r+   c                "    t          | d          S )N)belowabovenone)r   r8   s    r)   r:   zBokehPlotDirective.<lambda>   s    VA/I%J%J r+   c                $    t          |           d u S r6   r7   r8   s    r)   r:   zBokehPlotDirective.<lambda>   s    T!WW_ r+   )process-docstringsource-positionlinenosc                .   t          d          dk    rg S |                                 \  }}| j        j                            dd          }dt                      j         d| d}	 |                     |||          \  }}}}}n'# t          $ r}	t          d| d|	           d }	~	ww xY w| j        j
                            |t          | j        j                  f           | d	t          |           }
t          j        d
d
|
g          g}|                     |           | j                            dd          }|r|r|                     |d          ng }|                     ||          \  }}t+          ||          g}||z   |z   |z   |z   S )NBOKEH_SPHINX_QUICK1/-zbokeh-content-z.jszError generating z: 

. )idsr@   Fz<bokeh-content>)r#   r"   )r   process_args_or_contentenvdocnamereplacer	   hexprocess_source	Exceptionr   bokeh_plot_filesaddr   r   r
   targetprocess_sampledataoptionsgetparseprocess_code_blockr   )selfsourcepathdashed_docnamejs_filenamer"   js_path	docstringr#   e	target_idrT   process_docstringintror=   r<   autoloads                    r)   runzBokehPlotDirective.run   s   &''3..I3355)11#s;;Huww{HH^HHH	JDHDWDWX^`dfqDrDrAZ&)[[ 	J 	J 	JH+HHQHHIII	J!%%w8H0I0I&JKKK &;;(9(9;;	,r2I;7778''' L,,-@%HH<EcJ[c

9&7888ac..vyAAu#
SSST~%0588s   (B 
B*B%%B*r[   strr`   
str | Nonec                H   | j                             dd          }|dk    rg g fS t          ||                                          }| j                             dd          }t	          j        ||d|g           }t          | |           |dk    r|gg fS |dk    rg |gfS d S )	NrA   r<   r>   rB   Fpython)languagerB   classesr=   )rV   rW   _remove_module_docstringstripr
   literal_blockr   )rZ   r[   r`   source_positionrB   
code_blocks         r)   rY   z%BokehPlotDirective.process_code_block   s    ,**+<gFFf$$r6M)&)<<BBDD,""9e44((T[eghhh
j)))g%%<##g%%
|## &%r+   c           	     $   | j         r| j        rt          d          | j        rOt                              d| j        j                   | j        j        }d                    | j                  |fS | j         d         }t                              d| j        j        d|            |	                    d          r*t          t          |                    dd                    }n4|	                    d	          st          | j        j        j        |          }	 t          |          5 }|                                |fcd d d            S # 1 swxY w Y   d S # t           $ r'}t          d
|d| j        j        d|          d }~ww xY w)Nz7bokeh-plot:: directive can't have both args and contentz([bokeh-plot] handling inline content in 
r   z*[bokeh-plot] handling external content in z: z	__REPO__/rI   rF   zbokeh-plot:: error reading z for )	argumentscontentr   logdebugrL   rM   bokeh_plot_auxdirr   
startswithr   rN   appsrcdiropenreadrQ   )rZ   r\   fra   s       r)   rK   z*BokehPlotDirective.process_args_or_content   s   > 	Ydl 	YWXXX< 	1IIUAQUUVVV8-D99T\**D00~a 		[tx?O[[UY[[\\\??;'' 	3	4<<R#@#@AADD%% 	3+T22D	fd &qvvxx~& & & & & & & & & & & & & & & & & & 	f 	f 	fdDddIYdd_`ddeee	fs<   E .EE EE EE 
F("F

Fc                p   t          j                     t          ||| j                  \  }}|                                }t          | j        j        |          }t          |t          |          \  }}	t          |d          5 }
|

                    |           d d d            n# 1 swxY w Y   |	||||fS )Nw)r   _clear_extensions_evaluate_sourcerL   _sphinx_height_hintr   rx   r   	RESOURCESr|   write)rZ   r[   r\   r^   rootr`   r#   r_   jsr"   r~   s              r)   rP   z!BokehPlotDirective.process_source   s    !!!*64BBi..00tx1;??(y+FFJ'3 	1GGBKKK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 GVYDDs   B&&B*-B*c                    t          | j        d          sg | j        _        |                                 \  }}d|v r:|| j        j        vr-| j        j                            |           t          | j        d          sg | j        _        t          | j        d          sg | j        _        | j        j                            d| j        j        i           d}t          j	        ||          }|rt                      }|D ]D}|                    |d                             dd	                              d
                     E|D ]4}| j        j                            | j        j        |d           /d S d S d S d S )Nsolved_sampledataz/docs/examples/all_sampledata_xrefsall_gallery_overviewrM   z7(:|bokeh\.)sampledata(:|\.| import )\s*(\w+(\,\s*\w+)*)r4    rI   ,)rM   keyword)hasattrrL   r   get_source_infor%   r   r   rM   refindallsetupdaterN   split)	rZ   r[   filelinenoregexmatcheskeywordsmr   s	            r)   rU   z%BokehPlotDirective.process_sampledata   s   tx!455 	,)+DH&,,..f $$TX5O)O)OH&--d33348%;<< 302-48%;<< 302-H)0048+2    OEj//G 55  E EAOOAaDLLR$8$8$>$>s$C$CDDDD'  GH188#'8#3#*: :    % %$)O)O  r+   N)r[   rg   r`   rh   )r,   r-   r.   has_contentoptional_argumentsoption_specrf   rY   rK   rP   rU   r2   r+   r)   r   r      s        K 76JJ,, K9 9 9>$ $ $ $$f f f,E E E    r+   r   c                    t          | j        j        d          | j        _        t	          | j        j                   t          | j        d          st                      | j        _        d S d S )N
bokeh_plotrR   )r   rL   
doctreedirrx   r   r   r   rR   rz   s    r)   builder_initedr     sa     $SW%7 F FCGcg'(((37.// )#&55   ) )r+   c                   t          | j        j                  }t          |ddt	          |          | j        d           }|D ]\  }}t          | j        j        |t          |                    }t          t          |                     	 t          ||           [# t          $ r}t          d|d|           d }~ww xY wd S )Nzcopying bokeh-plot files... brownc                ,    t          | d                   S )Nr   )r   r8   s    r)   r:   z build_finished.<locals>.<lambda>&  s.      FN  OP  QR  OS  FT  FT r+   )stringify_funczcannot copy local file z
, reason: )sortedrL   rR   r   len	verbosityr   builderoutdirr   r   r   r   OSErrorr   )rz   	exceptionfiles
files_iterr   docpathrT   ra   s           r)   build_finishedr   $  s    37+,,E (FQTUZQ[Q[]`]j  |T  |T  U  U  UJ% O Owck('8D>>BB'&//"""	OT6"""" 	O 	O 	OMMM!MMNNN	OO Os   B
C)B>>Cc                0    |xj         |j         z  c_         d S r6   )rR   )rz   rL   docnamesothers       r)   env_merge_infor   0  s    E22r+   c                b   |                      dt                     |                     t          t          j                   |                     ddd           |                     dt                     |                     dt                     |                     dt                     t          S )	z+ Required Sphinx extension setup function. z
bokeh-plot)r1   bokeh_missing_google_api_key_okTr1   zbuilder-initedzbuild-finishedzenv-merge-info)add_directiver   add_noder   r1   add_config_valueconnectr   r   r   r   r   s    r)   r   r   3  s    l$6777LL';L<<<:D&IIIKK .111KK .111KK .111r+   r[   rg   returnc                    d| vr| S t           1|j        j        r|                     dd          S t	          d          |                     dt                     S )Nr   MISSING_API_KEYzThe GOOGLE_API_KEY environment variable is not set. Set GOOGLE_API_KEY to a valid API key, or set bokeh_missing_google_api_key_ok=True in conf.py to build anyway (with broken GMaps))r   configr   rN   r   )r[   rL   s     r)   _replace_google_api_keyr   D  sk    v%%:5 	G>>"24EFFFi
 
 	

 >>*N;;;r+   filenamec                @   t          | |          } t          | |          }t                      }t          j                    5  d|j        v rt          j        dt                     |                    |           d d d            n# 1 swxY w Y   |j	        rt          d|j         d|            t          |j                  dk    r$t          dt          |j                             |j        d	         |j        r|j                                        nd fS )
N)r[   r   	referenceignore)categoryzbokeh-plot:: error:

z

evaluating source:

r   z;bokeh-plot:: directive expects a single Document root, got r   )r   r   r   warningscatch_warningsrM   filterwarningsr   modify_documenterrorRuntimeErrorerror_detailr   rootsdocrn   )r[   r   rL   cds        r)   r   r   S  sV   $VS11Ffx888A

A 
	 	"	"  #+%%#H7NOOOO	!              
 	w igQ^gg_egghhh
17||qgY\]^]dYeYegghhh71:7qu{{}}}477s   :B		BBc                b    || S t          j        dt          j        |           dd|           S )Nz(\'\'\'|\"\"\")\s*z\s*(\'\'\'|\"\"\")rI   )r   subescape)r[   r`   s     r)   rm   rm   i  s9    6P	)(<(<PPPRTV\]]]r+   )r[   rg   r   rg   )r[   rg   r   rg   )=__doc__
__future__r   sphinx.utilr   	getLoggerr,   rv   r   r   osr   os.pathr   r   r   uuidr	   docutilsr
   docutils.parsers.rst.directivesr   r   sphinx.errorsr   r   r   r   sphinx.util.nodesr   bokeh.documentr   bokeh.embedr   bokeh.modelr   bokeh.util.warningsr   rI   r   bokeh_directiver   example_handlerr   utilr   r   __all__r   r   GeneralElementr   r   r   r   r   r   r   r   rm   r2   r+   r)   <module>r      s  G GX # " " " " "       g!! 
			        + + + + + + + + + +             8 8 8 8 8 8 8 8 % % % % % % < < < < < < < < < < - - - - - - $ # # # # # ' ' ' ' ' '       7 7 7 7 7 7       + + + + + + + + + + + + 1 1 1 1 1 1 1 1 ())  ""	% % % % %emU] % % %~ ~ ~ ~ ~ ~ ~ ~H) ) )
O 
O 
O3 3 3	 	 	"< < < <8 8 8 8,^ ^ ^ ^ ^r+   