gradio/guides/developing_faster_with_reload_mode.md
aliabid94 70ebf698fa
Live website changes (#1578)
* fix audio output cache (#804)

* fix audio output cache

* changes

* version update

Co-authored-by: Ali Abid <aliabid94@gmail.com>

* Website Tracker Slackbot (#797)

* added commands to reload script

* catch errors with git pull

* read new webhook from os variable

* correcting bash

* bash fixes

* formatting

* more robust error checking

* only sends success if git changes

* catching error from script

* escaping error text to send with curl

* correct text escaping for error message

* fix search bug in guides (#809)

* Update getting_started.md (#808)

* Fix type of server returned by `Launchable` (#810)

* `Launchable` returns a FastAPI now

* Update .gitignore

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 96f203108b]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42
Former-commit-id: bce6f9c4c5

* Add a missing line to getting started (#816)



Former-commit-id: 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 81e271ca22 [formerly 96f203108b]]]]
Former-commit-id: eaff13262853078e0c6c0baa54c731d9e56bc73f
Former-commit-id: b5112c3f42
Former-commit-id: bce6f9c4c5
Former-commit-id: feba0888e3

* Add a missing line to getting started (#816)

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc
Former-commit-id: 0d80e6a056

* Clean-History
- Remove 51MB file with this commit


Former-commit-id: 34b6a2325d613eeef622410f2d1ff3d869d3133c
Former-commit-id: dd700c33cc
Former-commit-id: 0d80e6a056
Former-commit-id: 20523b0519

* changes

* changes

* Homepage: header image size (#1347)

* image size

* image in local assets

* add dall-e mini banner

* undo ui changes

* changes

* changes

* updates

* updates

* changes

* changes

* changes

* h11 dependency

* add npm build-mac

* expand demo button to all classes

* add demos to docstrings

* add anchor tags to headers

* add required tag to param table

* add consistent styling for headers

* skip param beginning with underscore from docs

* skip kwargs param from docs

* remove types in param docstring

* override signature to reflect usage

* add supported events

* add step-by-step guides

* fix guide contribution link

* add related spaces

* fix img styling on guides

* pin quickstart, advanced, and block guides to top

* margin fix

* autogenerated copy buttons for all codeblocks

* changes

* documentaiton

* format

* launch

* formatting

* style changes

* remove backticks

* changes

* changes

Co-authored-by: Ali Abid <aliabid94@gmail.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
Co-authored-by: Julien Chaumond <julien@huggingface.co>
Co-authored-by: Ömer Faruk Özdemir <farukozderim@gmail.com>
Co-authored-by: Ali <ali.abid@huggingface.co>
Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2022-07-06 16:22:10 -07:00

4.8 KiB

Developing Faster with Auto-Reloading

Prerequisite: This Guide requires you to know about Blocks. Make sure to read the Guide to Blocks first.

Why Auto-Reloading?

When you are building a Gradio demo, particularly out of Blocks, you may find it cumbersome to keep re-running your code to test your changes.

To make it faster and more convenient to write your code, we've made it easier to "reload" your Gradio apps instantly when you are developing in a Python IDE (like VS Code, Sublime Text, PyCharm, or so on) or generally running your Python code from the terminal. We've also developed an analogous "magic command" that allows you to re-run cells faster if you use Jupyter Notebooks (or any similar environment like Colab).

This short Guide will cover both of these methods, so no matter how you write Python, you'll leave knowing how to build Gradio apps faster.

Python IDE Reload 🔥

If you are building Gradio Blocks using a Python IDE, your file of code (let's name it app.py) might looks something like this:

import gradio as gr

with gr.Blocks() as demo:
    gr.Markdown("# Greetings from Gradio!")
    inp = gr.Textbox(placeholder="What is your name?")
    out = gr.Textbox()

    inp.change(fn=lambda x: f"Welcome, {x}!", 
               inputs=inp, 
               outputs=out)

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

The problem is that anytime that you want to make a change to your layout, events, or components, you have to close and rerun your app by writing python app.py.

Instead of doing this, you can run your code in reload mode by changing 1 word: python to gradio:

In the terminal, run gradio app.py. That's it!

Now, you'll see that after you'll see something like this:

Launching in *reload mode* on: http://127.0.0.1:7860 (Press CTRL+C to quit)

Watching...

WARNING:  The --reload flag should not be used in production on Windows.

The important part here is the line that says Watching... What's happening here is that Gradio will be observing the directory where app.py file lives, and if the file changes, it will automatically rerun the file for you. So you can focus on writing your code, and your Gradio demo will refresh automatically 🥳

⚠️ Now, there is one important thing to keep in mind when use the reload mode: Gradio specifically looks for a Gradio Blocks/Interface demo called demo in your code. If you have named your demo something else, you can pass that as the 2nd parameter in your code, like this: gradio app.py my_demo

As a small aside, this auto-reloading happens if you change your app.py source code or the Gradio source code. Meaning that this can be useful if you decide to contribute to Gradio itself

Jupyter Notebook Magic 🔮

What about if you use Jupyter Notebooks (or Colab Notebooks, etc.) to develop code? We got something for you too!

We've developed a magic command that will create and run a Blocks demo for you. To use this, load the gradio extension at the top of your notebook:

%load_ext gradio

Then, in the cell that you are developing your Gradio demo, simply write the magic command %%blocks at the top, and then write the layout and components like you would normally:

%%blocks 

import gradio as gr

gr.Markdown("# Greetings from Gradio!")
inp = gr.Textbox(placeholder="What is your name?")
out = gr.Textbox()

inp.change(fn=lambda x: f"Welcome, {x}!", 
           inputs=inp, 
           outputs=out)

Notice that:

  • You do not need to put the boiler plate with gr.Blocks() as demo: and demo.launch() code — Gradio does that for you automatically!

  • Every time you rerun the cell, Gradio will re-launch your app on the same port and using the same underlying web server. This means you'll see your changes much, much faster than if you were rerunning the cell normally.

Here's what it looks like in a jupyter notebook:

🪄 This works in colab notebooks too! Here's a colab notebook where you can see the Blocks magic in action. Try making some changes and re-running the cell with the Gradio code!

The Notebook Magic is now the author's preferred way of building Gradio demos. Regardless of how you write Python code, we hope either of these methods will give you a much better development experience using Gradio.


Next Steps

Now that you know how to develop quickly using Gradio, start building your own!

If you are looking for inspiration, try exploring demos other people have built with Gradio, browse public Hugging Face Spaces 🤗