Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from datetime import datetime
|
3 |
+
import pytz
|
4 |
+
import time
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
js = """
|
8 |
+
function createGradioAnimation() {
|
9 |
+
var container = document.createElement('div');
|
10 |
+
container.id = 'gradio-animation';
|
11 |
+
container.style.fontSize = '2em';
|
12 |
+
container.style.fontWeight = 'bold';
|
13 |
+
container.style.textAlign = 'center';
|
14 |
+
container.style.marginBottom = '20px';
|
15 |
+
|
16 |
+
var text = 'Welcome to Code Tools!';
|
17 |
+
for (var i = 0; i < text.length; i++) {
|
18 |
+
(function(i){
|
19 |
+
setTimeout(function(){
|
20 |
+
var letter = document.createElement('span');
|
21 |
+
letter.style.opacity = '0';
|
22 |
+
letter.style.transition = 'opacity 0.5s';
|
23 |
+
letter.innerText = text[i];
|
24 |
+
|
25 |
+
container.appendChild(letter);
|
26 |
+
|
27 |
+
setTimeout(function() {
|
28 |
+
letter.style.opacity = '1';
|
29 |
+
}, 50);
|
30 |
+
}, i * 250);
|
31 |
+
})(i);
|
32 |
+
}
|
33 |
+
|
34 |
+
var gradioContainer = document.querySelector('.gradio-container');
|
35 |
+
gradioContainer.insertBefore(container, gradioContainer.firstChild);
|
36 |
+
|
37 |
+
return 'Animation created';
|
38 |
+
}
|
39 |
+
"""
|
40 |
+
|
41 |
+
|
42 |
+
def json_format(data):
|
43 |
+
try:
|
44 |
+
data = json.loads(data)
|
45 |
+
return data
|
46 |
+
except:
|
47 |
+
raise gr.Error("Json数据格式不正确!")
|
48 |
+
|
49 |
+
|
50 |
+
def html_format(data):
|
51 |
+
return data
|
52 |
+
|
53 |
+
|
54 |
+
def md_format(data):
|
55 |
+
return data
|
56 |
+
|
57 |
+
|
58 |
+
# 获取当前 Unix 时间戳
|
59 |
+
def get_current_timestamp(precision):
|
60 |
+
timestamp = int(time.time() * 1000) if precision == "ms" else int(time.time())
|
61 |
+
return str(timestamp)
|
62 |
+
|
63 |
+
|
64 |
+
# 时间戳转换为日期
|
65 |
+
def timestamp_to_date(input_time, target_timezone, precision):
|
66 |
+
try:
|
67 |
+
if precision == "ms":
|
68 |
+
input_time = int(input_time) / 1000 # 毫秒转为秒
|
69 |
+
else:
|
70 |
+
input_time = int(input_time) # 秒级时间戳
|
71 |
+
|
72 |
+
# 将输入的 Unix 时间戳转换为 UTC 时间
|
73 |
+
utc_time = datetime.utcfromtimestamp(input_time).replace(tzinfo=pytz.utc)
|
74 |
+
|
75 |
+
# 获取目标时区
|
76 |
+
target_tz = pytz.timezone(target_timezone)
|
77 |
+
|
78 |
+
# 转换到目标时区
|
79 |
+
target_time = utc_time.astimezone(target_tz)
|
80 |
+
|
81 |
+
# 返回格式化后的时间
|
82 |
+
return target_time.strftime("%Y-%m-%d %H:%M:%S")
|
83 |
+
except Exception as e:
|
84 |
+
return f"error: {e}"
|
85 |
+
|
86 |
+
|
87 |
+
# 日期转换为时间戳
|
88 |
+
def date_to_timestamp(input_date, target_timezone):
|
89 |
+
try:
|
90 |
+
# 解析日期输入为 datetime 对象
|
91 |
+
target_tz = pytz.timezone(target_timezone)
|
92 |
+
target_time = datetime.fromisoformat(input_date).astimezone(target_tz)
|
93 |
+
|
94 |
+
# 转换为 UTC 时间戳
|
95 |
+
timestamp = int(target_time.astimezone(pytz.utc).timestamp())
|
96 |
+
|
97 |
+
return str(timestamp)
|
98 |
+
except Exception as e:
|
99 |
+
return f"error: {e}"
|
100 |
+
|
101 |
+
|
102 |
+
# 获取所有时区列表
|
103 |
+
timezones = pytz.all_timezones
|
104 |
+
|
105 |
+
with gr.Blocks(title='Gradio-Tools', theme=gr.themes.Ocean(), js=js) as demo:
|
106 |
+
with gr.Tabs():
|
107 |
+
with gr.Tab("Json Format"):
|
108 |
+
with gr.Row():
|
109 |
+
json_input = gr.Textbox(label="Json Data", interactive=True, placeholder="{}", lines=50)
|
110 |
+
json_output = gr.Json(label="Json Format")
|
111 |
+
|
112 |
+
json_input.change(fn=json_format, inputs=json_input, outputs=json_output)
|
113 |
+
|
114 |
+
with gr.Tab("Timestamp Conversion"):
|
115 |
+
with gr.Row():
|
116 |
+
# 时间戳输入框,显示当前时间戳
|
117 |
+
timestamp_input = gr.Textbox(
|
118 |
+
label="Timestamp (s/ms)",
|
119 |
+
value=get_current_timestamp("s"), # 默认显示当前秒级时间戳
|
120 |
+
interactive=True, # 可编辑
|
121 |
+
elem_id="timestamp_input",
|
122 |
+
placeholder="Please enter a timestamp"
|
123 |
+
)
|
124 |
+
|
125 |
+
# 选择精度(秒或毫秒)
|
126 |
+
precision = gr.Dropdown(
|
127 |
+
["s", "ms"],
|
128 |
+
label="Timestamp accuracy",
|
129 |
+
value="s", # 默认秒
|
130 |
+
elem_id="precision"
|
131 |
+
)
|
132 |
+
|
133 |
+
target_timezone = gr.Dropdown(
|
134 |
+
timezones,
|
135 |
+
label="Target time zone",
|
136 |
+
value="Asia/Shanghai", # 默认设置为北京时间
|
137 |
+
elem_id="target_timezone"
|
138 |
+
)
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
# 日期输入框,用户输入日期
|
142 |
+
date_input = gr.Textbox(
|
143 |
+
label="Date Input (ISO 8601)",
|
144 |
+
placeholder="For example:2025-01-01 12:34:56",
|
145 |
+
elem_id="date_input"
|
146 |
+
)
|
147 |
+
|
148 |
+
output = gr.Textbox(label="Conversion results", elem_id="output")
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
# 时间戳转日期按钮
|
152 |
+
timestamp_to_date_btn = gr.Button("Timestamp to date", elem_id="timestamp_to_date_btn")
|
153 |
+
|
154 |
+
# 日期转时间戳按钮
|
155 |
+
date_to_timestamp_btn = gr.Button("Date to timestamp", elem_id="date_to_timestamp_btn")
|
156 |
+
|
157 |
+
# 精度选择改变时,动态更新时间戳显示
|
158 |
+
precision.change(
|
159 |
+
lambda precision: get_current_timestamp(precision),
|
160 |
+
inputs=precision,
|
161 |
+
outputs=timestamp_input
|
162 |
+
)
|
163 |
+
|
164 |
+
# 时间戳转日期按钮点击事件
|
165 |
+
def handle_timestamp_to_date(timestamp_input, target_timezone, precision):
|
166 |
+
if timestamp_input: # 如果时间戳输入框有内容
|
167 |
+
return timestamp_to_date(timestamp_input, target_timezone, precision)
|
168 |
+
return "Please enter a valid timestamp"
|
169 |
+
|
170 |
+
|
171 |
+
timestamp_to_date_btn.click(
|
172 |
+
handle_timestamp_to_date,
|
173 |
+
inputs=[timestamp_input, target_timezone, precision],
|
174 |
+
outputs=output
|
175 |
+
)
|
176 |
+
|
177 |
+
# 日期转时间戳按钮点击事件
|
178 |
+
def handle_date_to_timestamp(date_input, target_timezone):
|
179 |
+
if date_input: # 如果日期输入框有内容
|
180 |
+
return date_to_timestamp(date_input, target_timezone)
|
181 |
+
return "Please enter a valid date"
|
182 |
+
|
183 |
+
|
184 |
+
date_to_timestamp_btn.click(
|
185 |
+
handle_date_to_timestamp,
|
186 |
+
inputs=[date_input, target_timezone],
|
187 |
+
outputs=output
|
188 |
+
)
|
189 |
+
|
190 |
+
with gr.Tab("Html Rendering"):
|
191 |
+
with gr.Row():
|
192 |
+
html_input = gr.Textbox(label="Html Data", interactive=True, placeholder="<html></html>", lines=50)
|
193 |
+
html_output = gr.HTML()
|
194 |
+
|
195 |
+
html_input.change(fn=html_format, inputs=html_input, outputs=html_output)
|
196 |
+
|
197 |
+
with gr.Tab("Markdown Rendering"):
|
198 |
+
with gr.Row():
|
199 |
+
md_input = gr.Textbox(label="Markdown Data", interactive=True, placeholder="## title", lines=50)
|
200 |
+
md_output = gr.Markdown()
|
201 |
+
|
202 |
+
md_input.change(fn=md_format, inputs=md_input, outputs=md_output)
|
203 |
+
|
204 |
+
demo.launch()
|