-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Embedding Components on Docs #1726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
64eac92
e2c220c
6748e68
643a32d
eb7a873
822dd5b
8ecc76b
1a4fc1f
f78cd53
c31f73f
f33e211
8e50dd4
00a36a6
cd5c02e
0943f66
fc5c446
7bac843
d74ff1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| matplotlib | ||
| numpy |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # This file is used to embed components in gradio.app/docs | ||
|
|
||
| import gradio as gr | ||
|
|
||
|
|
||
| with gr.Blocks() as Textbox_demo: | ||
| gr.Textbox() | ||
|
|
||
| with gr.Blocks() as Number_demo: | ||
| gr.Number() | ||
|
|
||
| with gr.Blocks() as Slider_demo: | ||
| gr.Slider() | ||
|
|
||
| with gr.Blocks() as Checkbox_demo: | ||
| gr.Checkbox() | ||
|
|
||
| with gr.Blocks() as CheckboxGroup_demo: | ||
| gr.CheckboxGroup(choices=["First Choice", "Second Choice", "Third Choice"]) | ||
|
|
||
| with gr.Blocks() as Radio_demo: | ||
| gr.Radio(choices=["First Choice", "Second Choice", "Third Choice"]) | ||
|
|
||
| with gr.Blocks() as Dropdown_demo: | ||
| gr.Dropdown(choices=["First Choice", "Second Choice", "Third Choice"]) | ||
|
|
||
| with gr.Blocks() as Image_demo: | ||
| gr.Image() | ||
|
|
||
| with gr.Blocks() as Video_demo: | ||
| gr.Video() | ||
|
|
||
| with gr.Blocks() as Audio_demo: | ||
| gr.Audio() | ||
|
|
||
| with gr.Blocks() as File_demo: | ||
| gr.File() | ||
|
|
||
| with gr.Blocks() as Dataframe_demo: | ||
| gr.Dataframe(interactive=True) | ||
|
|
||
| with gr.Blocks() as Timeseries_demo: | ||
| gr.Timeseries() | ||
|
|
||
| with gr.Blocks() as Variable_demo: | ||
| gr.Variable() | ||
|
|
||
| with gr.Blocks() as Button_demo: | ||
| gr.Button() | ||
|
|
||
| with gr.Blocks() as ColorPicker_demo: | ||
| gr.ColorPicker() | ||
|
|
||
| with gr.Blocks() as Label_demo: | ||
| gr.Label(value={"First Label": 0.7, "Second Label": 0.2, "Third Label": 0.1}) | ||
|
|
||
| with gr.Blocks() as HighlightedText_demo: | ||
| gr.HighlightedText(value=[("Text","Label 1"),("to be","Label 2"),("highlighted","Label 3")]) | ||
|
|
||
| with gr.Blocks() as JSON_demo: | ||
| gr.JSON(value={"Key 1": "Value 1", "Key 2": {"Key 3": "Value 2", "Key 4": "Value 3"}, "Key 5": ["Item 1", "Item 2", "Item 3"]}) | ||
|
|
||
| with gr.Blocks() as HTML_demo: | ||
| gr.HTML(value="<p style='margin-top: 1rem, margin-bottom: 1rem'>Gradio Docs Readers: <img src='https://visitor-badge.glitch.me/badge?page_id=gradio-docs-visitor-badge' alt='visitor badge' style='display: inline-block'/></p>") | ||
|
|
||
| with gr.Blocks() as Gallery_demo: | ||
| cheetahs = [ | ||
| "https://upload.wikimedia.org/wikipedia/commons/0/09/TheCheethcat.jpg", | ||
| "https://nationalzoo.si.edu/sites/default/files/animals/cheetah-003.jpg", | ||
| "https://img.etimg.com/thumb/msid-50159822,width-650,imgsize-129520,,resizemode-4,quality-100/.jpg", | ||
| "https://nationalzoo.si.edu/sites/default/files/animals/cheetah-002.jpg", | ||
| "https://images.theconversation.com/files/375893/original/file-20201218-13-a8h8uq.jpg?ixlib=rb-1.1.0&rect=16%2C407%2C5515%2C2924&q=45&auto=format&w=496&fit=clip", | ||
| "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQeSdQE5kHykTdB970YGSW3AsF6MHHZzY4QiQ&usqp=CAU", | ||
| "https://www.lifegate.com/app/uploads/ghepardo-primo-piano.jpg", | ||
| "https://i.natgeofe.com/n/60004bcc-cd85-4401-8bfa-6f96551557db/cheetah-extinction-3_3x4.jpg", | ||
| "https://qph.cf2.quoracdn.net/main-qimg-0bbf31c18a22178cb7a8dd53640a3d05-lq" | ||
| ] | ||
| gr.Gallery(value=cheetahs) | ||
|
|
||
| with gr.Blocks() as Chatbot_demo: | ||
| gr.Chatbot(value=[["Hello World","Hey Gradio!"],["❤️","😍"],["🔥","🤗"]]) | ||
|
|
||
| with gr.Blocks() as Model3D_demo: | ||
| gr.Model3D() | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
|
|
||
| Fs = 8000 | ||
| f = 5 | ||
| sample = 8000 | ||
| x = np.arange(sample) | ||
| y = np.sin(2 * np.pi * f * x / Fs) | ||
| plt.plot(x, y) | ||
|
|
||
| with gr.Blocks() as Plot_demo: | ||
| gr.Plot(value=plt) | ||
|
|
||
| with gr.Blocks() as Markdown_demo: | ||
| gr.Markdown(value="This _example_ was **written** in [Markdown](https://en.wikipedia.org/wiki/Markdown)\n") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,22 +22,39 @@ | |
| def launch_demo(demo_folder): | ||
| subprocess.check_call([f"cd {demo_folder} && python run2.py"], shell=True) | ||
|
|
||
| SUFFIX = "_component" | ||
| COMPONENTS_DEMOS_FOLDER = os.path.join(GRADIO_DEMO_DIR, 'components_demos') | ||
| start_launch_time = time.time() | ||
| for demo_name, port in demo_port_sets: | ||
| demo_folder = os.path.join(GRADIO_DEMO_DIR, demo_name) | ||
| demo_file = os.path.join(demo_folder, "run.py") | ||
| demo_2_file = os.path.join(demo_folder, "run2.py") | ||
| with open(demo_file, "r") as file: | ||
| filedata = file.read() | ||
| assert "demo.launch()" in filedata, demo_name + " has no demo.launch()\n" + filedata | ||
| filedata = filedata.replace(f"demo.launch()", f"demo.launch(server_port={port}, _frontend=False)") | ||
| with open(demo_2_file, "w") as file: | ||
| file.write(filedata) | ||
| if demo_name.endswith(SUFFIX): | ||
| demo_folder = COMPONENTS_DEMOS_FOLDER | ||
| demo_file = os.path.join(demo_folder, "run.py") | ||
| component_folder = os.path.join(GRADIO_DEMO_DIR, demo_name) | ||
| os.mkdir(component_folder) | ||
| demo_2_file = os.path.join(component_folder, "run2.py") | ||
| with open(demo_file, "r") as file: | ||
| filedata = file.read() | ||
| filedata += f"\n\n{demo_name[:-len(SUFFIX)]}_demo.launch(server_port={port}, _frontend=False)" | ||
| with open(demo_2_file, "w") as file: | ||
| file.write(filedata) | ||
| demo_thread = threading.Thread(target=launch_demo, args=(component_folder,)) | ||
|
|
||
| else: | ||
| demo_folder = os.path.join(GRADIO_DEMO_DIR, demo_name) | ||
| demo_file = os.path.join(demo_folder, "run.py") | ||
| demo_2_file = os.path.join(demo_folder, "run2.py") | ||
| with open(demo_file, "r") as file: | ||
| filedata = file.read() | ||
| assert "demo.launch()" in filedata, demo_name + " has no demo.launch()\n" + filedata | ||
| filedata = filedata.replace(f"demo.launch()", f"demo.launch(server_port={port}, _frontend=False)") | ||
| with open(demo_2_file, "w") as file: | ||
| file.write(filedata) | ||
| demo_thread = threading.Thread(target=launch_demo, args=(demo_folder,)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some duplicated code across this if/else branches |
||
| time_to_up[demo_name] = -(time.time()) | ||
| demo_thread = threading.Thread(target=launch_demo, args=(demo_folder,)) | ||
| demo_thread.start() | ||
| demo_threads[demo_name] = demo_thread | ||
|
|
||
|
|
||
| print("launch time:", time.time() - start_launch_time) | ||
|
|
||
| while True: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -245,6 +245,19 @@ <h3 class="text-3xl font-light my-4" id="block-layouts">Block Layouts</h3> | |
| addCopyButtons(navigator.clipboard); | ||
| }; | ||
|
|
||
|
|
||
| // Remove built-with-gradio footers and extra space from embedded components | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. couldn't be done with css? also this needs to be done in guides too, maybe put this in a js file or common template so it can be used there too.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do this in a separate PR |
||
| window.addEventListener('load', function () { | ||
| document.querySelectorAll('.embedded-component gradio-app').forEach(g => { | ||
| let shadowroot = g.shadowRoot; | ||
| let f = shadowroot.querySelector('footer'); | ||
| f.classList.add('hidden'); | ||
| let c = shadowroot.querySelector('.gradio-container'); | ||
| c.style.removeProperty('min-height'); | ||
| } | ||
| ); | ||
| }) | ||
|
|
||
| </script> | ||
| </body> | ||
| </html> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better name, COMPONENT_SUFFIX?