gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
29.49k stars 2.19k forks source link

Hot reloads break when loading external CSS #8192

Closed jameszhou02 closed 2 weeks ago

jameszhou02 commented 2 weeks ago

Describe the bug

When launching a gradio app with hot reloading, the initial load works but subsequent changes to CSS files and components using external CSS break the hot reload.

Have you searched existing issues? πŸ”Ž

Reproduction

test.py

import gradio as gr
import os

STYLES_PATH = os.path.join(os.path.dirname(__file__), "styles.css")

with gr.Blocks(
    css=STYLES_PATH,
) as test_demo:
    with gr.Blocks():
        gr.Markdown(
            """
            ## Hello world
            """
        )

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

styles.css

html {
  background-color: white;
}

Run command: gradio test.py --demo-name=test_demo

Then change background-color to any other color, or add own CSS.

Screenshot

No response

Logs

Error output (path is just placeholder for where I was testing this python script):

gradio test.py --demo-name=test_demo
Watching: '[path]' '[path]'

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.

Changes detected in: [path]/styles.css
Reloading test failed with the following exception:
Traceback (most recent call last):
  File "/Users/james/anaconda3/lib/python3.10/site-packages/gradio/utils.py", line 264, in watchfn
    changed_in_copy = _remove_no_reload_codeblocks(str(changed))
  File "/Users/james/anaconda3/lib/python3.10/site-packages/gradio/utils.py", line 173, in _remove_no_reload_codeblocks
    tree = ast.parse(code)
  File "/Users/james/anaconda3/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    html {
         ^
SyntaxError: invalid syntax

### System Info

```shell
gradio version: 4.28.3
python version: 3.10.13

Severity

I can work around it

jameszhou02 commented 2 weeks ago

Same error with python version: 3.11.7

freddyaboulton commented 2 weeks ago

Thanks @jameszhou02 ! This should be a straightforward fix. We need to skip lines 264 to 272 in this file if the source file that changed is not a python file.

Would you like to open a PR?

jameszhou02 commented 2 weeks ago

On it!

jameszhou02 commented 2 weeks ago

Submitted a PR that's passing all tests here: https://github.com/gradio-app/gradio/pull/8194

Was running into local issues when messing w/ hot-reloading but they're really minor. Essentially changes are detected but when the hot reload occurs seems to be dynamic. Here's a video of what was happening---wasn't sure if this is the intended behavior because it seems inconsistent.

https://github.com/gradio-app/gradio/assets/61927718/9ecb1643-c264-41bb-8f73-b77cede7e23c

freddyaboulton commented 2 weeks ago

Hmm I think that may be an unrelated bug. Will review your PR shortly

freddyaboulton commented 1 week ago

Hi @jameszhou02 , that issue you saw where the textbox value was not updated in reload mode has been fixed in #8227