gradio/demo/code/run.ipynb
Archit-Kohli 921716f618
Fixed visibility issue for all notebooks on GitHub (#5917)
* fixed visibility error in notebooks in github

* Delete fixNotebooks.py

deleted script used to fix notebooks

* Update generate_notebooks.py

fixed a small bug that prevented visibility of notebooks in GitHub
2023-10-15 18:16:57 -07:00

2.0 KiB

Gradio Demo: code

In [ ]:
!pip install -q gradio 
In [ ]:
# Downloading files from the demo repo
import os
!wget -q https://github.com/gradio-app/gradio/raw/main/demo/code/file.css
In [ ]:
import gradio as gr
import os
from time import sleep


css_file = os.path.join(os.path.abspath(''), "file.css")


def set_lang(language):
    print(language)
    return gr.Code(language=language)


def set_lang_from_path():
    sleep(1)
    return gr.Code((css_file,), language="css")


def code(language, code):
    return gr.Code(code, language=language)


io = gr.Interface(lambda x: x, "code", "code")

with gr.Blocks() as demo:
    lang = gr.Dropdown(value="python", choices=gr.Code.languages)
    with gr.Row():
        code_in = gr.Code(
            language="python",
            label="Input",
            value='def all_odd_elements(sequence):\n    """Returns every odd element of the sequence."""',
        )
        code_out = gr.Code(label="Output")
    btn = gr.Button("Run")
    btn_two = gr.Button("Load File")

    lang.change(set_lang, inputs=lang, outputs=code_in)
    btn.click(code, inputs=[lang, code_in], outputs=code_out)
    btn_two.click(set_lang_from_path, inputs=None, outputs=code_out)
    io.render()

if __name__ == "__main__":
    demo.launch()