File size: 1,696 Bytes
b1be69e 466dfc4 b1be69e 466dfc4 b1be69e 466dfc4 b1be69e |
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 |
import gradio as gr
# Define the pre-stored text snippets
snippet1 = "This is the first predefined text snippet."
snippet2 = "This is the second predefined text snippet."
# Function to insert snippet at a given position
def insert_snippet(text, snippet, position):
return text[:position] + snippet + text[position:]
with gr.Blocks() as demo:
gr.Markdown("# Blog Writing Helper")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(lines=10, placeholder="Write your blog here...", label="Text Editor")
button1 = gr.Button("Insert Snippet 1")
button2 = gr.Button("Insert Snippet 2")
with gr.Column():
gr.Markdown("**Live Preview**")
with gr.Box():
markdown_preview = gr.Markdown(label="Live Preview")
button1.click(lambda text, position: insert_snippet(text, snippet1, position), inputs=[text_input, gr.State(0)], outputs=text_input)
button2.click(lambda text, position: insert_snippet(text, snippet2, position), inputs=[text_input, gr.State(0)], outputs=text_input)
text_input.change(lambda text: text, inputs=text_input, outputs=markdown_preview)
# Add custom JavaScript to get cursor position
js_code = """
<script>
let textbox = document.querySelector('textarea');
let position = 0;
textbox.addEventListener('click', () => {
position = textbox.selectionStart;
});
textbox.addEventListener('keyup', () => {
position = textbox.selectionStart;
});
gradio.input('state', position);
</script>
"""
gr.HTML(js_code)
demo.launch()
|