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
32.08k stars 2.4k forks source link

An error occurs when passing `datetime.datetime` to `gr.DateTime` #8889

Open hysts opened 1 month ago

hysts commented 1 month ago

Describe the bug

datetime.datetime is allowed as the value of gr.DateTime according to the docstring, but an error occurs when passing it.

Looks like postprocess() is called when calling super().__init__(), so this https://github.com/gradio-app/gradio/blob/ac132e3cbc8dbc7bec3d607d52bef347e90feb41/gradio/components/datetime.py#L77-L80 should come before https://github.com/gradio-app/gradio/blob/ac132e3cbc8dbc7bec3d607d52bef347e90feb41/gradio/components/datetime.py#L63-L76

Have you searched existing issues? 🔎

Reproduction

import datetime

import gradio as gr

with gr.Blocks() as demo:
    dt = gr.DateTime(
        type="datetime",
        value=datetime.datetime.now(tz=datetime.timezone.utc),
    )
demo.queue().launch()

Screenshot

No response

Logs

Traceback (most recent call last):
  File "/home/ubuntu/repos/temp/2024-07-24-datetime/app.py", line 6, in <module>
    dt = gr.DateTime(
  File "/home/ubuntu/.virtualenvs/py31014/lib/python3.10/site-packages/gradio/component_meta.py", line 163, in wrapper
    return fn(self, **kwargs)
  File "/home/ubuntu/.virtualenvs/py31014/lib/python3.10/site-packages/gradio/components/datetime.py", line 63, in __init__
    super().__init__(
  File "/home/ubuntu/.virtualenvs/py31014/lib/python3.10/site-packages/gradio/component_meta.py", line 163, in wrapper
    return fn(self, **kwargs)
  File "/home/ubuntu/.virtualenvs/py31014/lib/python3.10/site-packages/gradio/components/base.py", line 211, in __init__
    initial_value = self.postprocess(initial_value)
  File "/home/ubuntu/.virtualenvs/py31014/lib/python3.10/site-packages/gradio/components/datetime.py", line 112, in postprocess
    return datetime.strftime(value, self.time_format)
AttributeError: 'DateTime' object has no attribute 'time_format'

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 4.39.0
gradio_client version: 1.1.1

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 22.1.0
anyio: 3.7.1
fastapi: 0.111.0
ffmpy: 0.3.2
gradio-client==1.1.1 is not installed.
httpx: 0.27.0
huggingface-hub: 0.24.1
importlib-resources: 6.4.0
jinja2: 3.1.4
markupsafe: 2.1.5
matplotlib: 3.9.1
numpy: 1.26.4
orjson: 3.10.6
packaging: 24.1
pandas: 2.2.2
pillow: 10.4.0
pydantic: 2.8.2
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.5.1
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.1
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.5.0
httpx: 0.27.0
huggingface-hub: 0.24.1
packaging: 24.1
typing-extensions: 4.12.2
websockets: 11.0.3

Severity

I can work around it

zwldarren commented 4 weeks ago

I think you should use strftime():

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")