o
    ٷi5                     @  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	 d dl
mZ d dlZdd ZdZdZd	Zd
ZdZdZejG dd dZi deddeddeddeddededdededdededdededdededdededdededdedddedd ed d!d"edd#ed#d$d%d&ed&d'd"edd(ed(d)d*ed+dd,ed,ddedd-ed-d.d"edd/ed/d0d"eded1ddeded2d3eded4d5eded6eded7eded8ed9ded:eded;ed<ded=d>ed?d@edAdBdCdDedEdFdGdDedHdIdJdDedKdLed9d@edMdNdOedPdQdRZdwdVdWZdxd\d]ZdydcddZdzdhdiZd{dldmZd|dqdrZd}dsdtZd{dudvZdS )~    )annotationsN)Path)Literalc                   C  s   t  jdd dkS )zHCheck if the current working directory ends with gradio/js/preview/test.N)gradiojspreviewtest)r   cwdparts r   r   `/home/ubuntu/.local/lib/python3.10/site-packages/gradio/cli/commands/components/_create_utils.py_in_test_dir   s   r   a  
example = {name}().example_value()

demo = gr.Interface(
    lambda x:x,
    {name}(),  # interactive version of your component
    {name}(),  # static version of your component
    # examples=[[example]],  # uncomment this line to view the "example version" of your component
)
z
example = {name}().example_value()

with gr.Blocks() as demo:
    with gr.Row():
        {name}(label="Blank"),  # blank component
        {name}(value=example, label="Populated"),  # populated component
z
with gr.Blocks() as demo:
    with {name}():
        gr.Textbox(value="foo", interactive=True)
        gr.Number(value=10, interactive=True)
z
with gr.Blocks() as demo:
    gr.Markdown("# Change the value (keep it JSON) and the front-end will update automatically.")
    {name}(value={{"message": "Hello from Gradio!"}}, label="Static")
zgradio-template-\w+zgradio-template-{template}c                   @  sB   e Zd ZU ded< eZded< dZded< dZded< dd Zd	S )
ComponentFilesstrtemplate	demo_code python_file_namejs_dirc                 C  s.   | j p| j | _ | jp| j  d| _d S )N.py)r   r   lowerr   )selfr   r   r   __post_init__A   s   zComponentFiles.__post_init__N)	__name__
__module____qualname____annotations__default_demo_coder   r   r   r   r   r   r   r   r   :   s   
 r   AnnotatedImagezannotated_image.py)r   r   r   HighlightedTextzhighlighted_text.pyChatbot)r   r   GalleryHTMLLabelMarkdownFallbackPlotBarPlotznative_plot.pyplot)r   r   r   r   ClearButtonzclear_button.pybuttonColorPickerzcolor_picker.py)r   r   DuplicateButtonzduplicate_button.pyFileExplorerzfile_explorer.pyfileexplorerz|
        import os

        with gr.Blocks() as demo:
            {name}(value=os.path.dirname(__file__).split(os.sep))
    LinePlotLogoutButtonzlogout_button.pyLoginButtonzlogin_button.pyScatterPlotUploadButtonzupload_button.pyJSONzjson_component.pyRowColumnTabsa
  
        with gr.Blocks() as demo:
            with {name}():
                with gr.Tab("Tab 1"):
                    gr.Textbox(value="foo", interactive=True)
                with gr.Tab("Tab 2"):
                    gr.Number(value=10, interactive=True)
        Group	Accordionz
        with gr.Blocks() as demo:
            with {name}(label="Accordion"):
                gr.Textbox(value="foo", interactive=True)
                gr.Number(value=10, interactive=True)
        Model3Dmodel3Dz@
        with gr.Blocks() as demo:
            {name}()
        )r   r   r   ImageEditorzimage_editor.pyimageeditor)r   r   r   MultimodalTextboxzmultimodal_textbox.pymultimodaltextboxDownloadButtonzdownload_button.pydownloadbuttonWalkthroughtabsSteptabitemzwalkthrough.pyz
        with gr.Blocks() as demo:
            with {name}():
                gr.Textbox(value="foo", interactive=True)
        )r   r   r   r   )r3   r4   r5   r6   r7   r8   r9   r:   r;   r=   r?   rA   rC   rE   r   
str | Nonereturnc                 C  s4   | pd} | t v rt |  S t|   d|  | dS )Nr&   r   )r   r   r   )	OVERRIDESr   r   r   r   r   r   _get_component_code   s   rK   namer   local_js_dirr   c                 C  s,   t t|| dd  d  }|d S )N/   package.jsonversion)jsonloadsr   split	read_text)rL   rM   package_jsonr   r   r   _get_js_dependency_version  s   rW   rV   dictkey*Literal['dependencies', 'devDependencies']
gradio_dirc                 C  s<   |  |g D ]}t s|drt||d | | |< q| S )Nz@gradio/_frontend_code)getr   
startswithrW   )rV   rY   r[   depr   r   r   _modify_js_deps	  s   r`   	directory
str | PathNonec                 C  s@   t | }|dD ]}| r|  q	| rt| q	dS )zADelete all contents of a directory, but not the directory itself.*N)r   globis_fileunlinkis_dirshutilrmtree)ra   pathchildr   r   r   delete_contents  s   

rm   	componentpackage_namec                 C  s   |d }|j dd tttj}dd }tjt|d |j	 |d|d t
t|d  }||d	< t|d
|}t|d|}|d t
j|dd ttttjd d |d  ttttjd d |d  d S )NfrontendT)exist_okc                 S  sH   g }|D ]}| ds| dsd|v sd|v sd|v r!|| q|S )N	CHANGELOG	README.mdz.test.z	.stories.z.spec.)r^   append)_srcnamesignorednr   r   r   ignore,  s   
z _create_frontend.<locals>.ignorer\   )dirs_exist_okry   rP   rL   dependenciesdevDependencies   )indentfileszgradio.config.jsztsconfig.json)mkdirr   inspectgetfiler   parentri   copytreer   r   rR   rS   rU   r`   
write_textdumpscopy__file__)rL   rn   ra   ro   rp   pry   source_package_jsonr   r   r   _create_frontend!  s.   r   old_class_namenew_class_namecontentc                 C  s    dt |  d}t |||S )Nz(?<=\b)(?<!\bimport\s)(?<!\.)z(?=\b))reescapesub)r   r   r   patternr   r   r   _replace_old_class_nameN  s   r   c                 C  s   d dd | dD S )N
c                 S  s   g | ]	}| d s|qS )z
@document()r^   ).0liner   r   r   
<listcomp>U  s    z)_strip_document_lines.<locals>.<listcomp>)joinrT   )r   r   r   r   _strip_document_linesS  s   r   c              
   C  s  dd }t jjdft jjdft jjdfg}d }d }|D ]\}}	||j|}|r+|	} nq|s7td|j d|s=tdtd	j	|| d
}
|d 
|
 |d | }|jddd ttjd d }| }|d }|
| ttjd d }| }|d }|d|dtj	|d}|
| |d }|jddd |d 
d| d|  d|jj	| d d |d   |d }|
d|   d|  d |  d! ttt j}||   d" }tt|| |j t| || |jd"d# }||   d# }| rtt|t| | }t|| |}t|}|
| | rA| }t|| |}t|}|
| d S d S )$Nc                 S  s&   |D ]}|   |  kr|  S qd S )N)r   )r   list_to_searchitemr   r   r   find_template_in_list\  s
   z._create_backend.<locals>.find_template_in_list
componentslayouts_simple_templateszCannot find z in gradio.components, gradio.layouts, or gradio._simple_templates. Please pass in a valid component name via the --template option. It must match the name of the python class.zModule not foundz
# {package_name}
A Custom Gradio component.

## Example usage

```python
import gradio as gr
from {package_name} import {name}
```
)ro   rL   rs   backendT)rq   parentsr   	gitignorez
.gitignorezpyproject_.tomlzpyproject.tomlz<<name>>z<<template>>rJ   demozapp.pyz
import gradio as gr
from z import z

)rL   z/

if __name__ == "__main__":
    demo.launch()
z__init__.pyz
from .z

__all__ = ['z']
r   z.pyi)r   r   __all__r   r   r   
ValueErrortextwrapdedentformatr   r   r   r   r   rU   replacePATTERNr   touchr   r   r   ri   r   r   r   existsr   r   )rL   rn   ra   ro   r   lists_to_searchcorrect_cased_templatemodulelist_module_namereadme_contentsr   r   gitignore_contentsgitignore_dest	pyprojectpyproject_contentspyproject_destdemo_dirinitr   python_filesource_pyi_filepyi_filer   pyi_contentr   r   r   _create_backendY  s   







r   )r   rG   rH   r   )rL   r   rM   r   rH   r   )rV   rX   rY   rZ   r[   r   )ra   rb   rH   rc   )rL   r   rn   r   ra   r   ro   r   )r   r   r   r   r   r   )r   r   ) 
__future__r   dataclassesr   rR   r   ri   r   pathlibr   typingr   r   r   r   static_only_demo_codelayout_demo_codefallback_code
PATTERN_REr   	dataclassr   r   rI   rK   rW   r`   rm   r   r   r   r   r   r   r   r   <module>   sz   	!'4:@F
  
1





-
