File size: 1,437 Bytes
2555ffe
 
6a0b641
 
2555ffe
6a0b641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2555ffe
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python

import datetime

import gradio as gr
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go

from gradio_calendar import Calendar

DESCRIPTION = """
- [GitHub](https://github.com/freddyaboulton/gradio-calendar)
- [PyPI](https://pypi.org/project/gradio-calendar/)
"""

np.random.seed(1)
data = np.random.normal(size=(500, 5)).cumsum(axis=0)
df = pd.DataFrame(data, columns=["A", "B", "C", "D", "E"])
df.index = pd.to_datetime(pd.date_range("2024-01-01", periods=500))


def update(start_date: datetime.datetime, end_date: datetime.datetime) -> go.Figure:
    fig = px.line(df)
    fig.update_layout(xaxis_range=[start_date, end_date], xaxis_rangeslider_visible=True)
    return fig


with gr.Blocks(css="style.css") as demo:
    with gr.Row():
        start_date = Calendar(
            label="Start date",
            type="datetime",
            value=datetime.datetime(2024, 1, 1),
        )
        end_date = Calendar(
            label="End date",
            type="datetime",
            value=datetime.datetime(2024, 3, 31),
        )
    plot = gr.Plot()

    gr.on(
        triggers=[start_date.change, end_date.change],
        fn=update,
        inputs=[start_date, end_date],
        outputs=plot,
    )
    demo.load(fn=update, inputs=[start_date, end_date], outputs=plot)

    gr.Markdown(DESCRIPTION)


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