Url-shortner2 / app.py
honey90's picture
Update app.py
bb2bb24 verified
import gradio as gr
import pyshorteners
def shorten_url(url):
# pyshorteners 라이브러리를 사용하여 URL을 짧게 만드는 함수
shortener = pyshorteners.Shortener()
short_url = shortener.tinyurl.short(url)
return short_url
def url_shortener(url):
# 사용자가 입력한 URL을 받아 짧게 만들고 그 결과를 반환
short_url = shorten_url(url)
return short_url
def clear_inputs():
return "", ""
# Gradio 인터페이스 설정
with gr.Blocks() as iface:
with gr.Row():
with gr.Column():
url_input = gr.Textbox(label="Enter URL")
shorten_button = gr.Button("Shorten URL")
clear_button = gr.Button("Clear")
with gr.Column():
short_url_output = gr.Textbox(label="Shortened URL", copyable=True)
copy_button = gr.Button("Copy to Clipboard")
def update_short_url(url):
short_url = url_shortener(url)
return short_url
shorten_button.click(fn=update_short_url, inputs=url_input, outputs=short_url_output)
clear_button.click(fn=clear_inputs, inputs=[], outputs=[url_input, short_url_output])
copy_button.click(fn=None, _js="""
const shortUrlInput = document.querySelector('input[aria-label="Shortened URL"]');
navigator.clipboard.writeText(shortUrlInput.value).then(() => {
alert('URL copied to clipboard');
}).catch(err => {
console.error('Error copying text: ', err);
});
""")
iface.launch()