Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
#
|
4 |
-
snippet1 = "
|
5 |
-
snippet2 = "
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
)
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
with gr.Row():
|
21 |
with gr.Column():
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
insert_button2 = gr.Button("Insert Snippet 2")
|
26 |
with gr.Column():
|
27 |
-
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Define the pre-stored text snippets
|
4 |
+
snippet1 = "This is the first predefined text snippet."
|
5 |
+
snippet2 = "This is the second predefined text snippet."
|
6 |
|
7 |
+
# Function to insert snippet at a given position
|
8 |
+
def insert_snippet(text, snippet, position):
|
9 |
+
return text[:position] + snippet + text[position:]
|
10 |
|
11 |
+
# JavaScript to get and set cursor position
|
12 |
+
js_code = """
|
13 |
+
<script>
|
14 |
+
let position = 0;
|
15 |
+
function updatePosition() {
|
16 |
+
let textbox = document.querySelector('textarea');
|
17 |
+
position = textbox.selectionStart;
|
18 |
+
}
|
19 |
+
function insertAtCursor(snippet) {
|
20 |
+
let textbox = document.querySelector('textarea');
|
21 |
+
let text = textbox.value;
|
22 |
+
let newText = text.slice(0, position) + snippet + text.slice(position);
|
23 |
+
textbox.value = newText;
|
24 |
+
position += snippet.length;
|
25 |
+
textbox.focus();
|
26 |
+
}
|
27 |
+
document.querySelector('textarea').addEventListener('click', updatePosition);
|
28 |
+
document.querySelector('textarea').addEventListener('keyup', updatePosition);
|
29 |
+
document.querySelector('#button1').addEventListener('click', function() { insertAtCursor("This is the first predefined text snippet."); });
|
30 |
+
document.querySelector('#button2').addEventListener('click', function() { insertAtCursor("This is the second predefined text snippet."); });
|
31 |
+
</script>
|
32 |
+
"""
|
33 |
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("# Blog Writing Helper")
|
36 |
+
|
37 |
with gr.Row():
|
38 |
with gr.Column():
|
39 |
+
text_input = gr.Textbox(lines=10, placeholder="Write your blog here...", label="Text Editor")
|
40 |
+
button1 = gr.Button("Insert Snippet 1", elem_id="button1")
|
41 |
+
button2 = gr.Button("Insert Snippet 2", elem_id="button2")
|
|
|
42 |
with gr.Column():
|
43 |
+
gr.Markdown("**Live Preview**")
|
44 |
+
markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")
|
45 |
|
46 |
+
# Live preview update
|
47 |
+
text_input.change(lambda text: text, inputs=text_input, outputs=markdown_preview)
|
48 |
+
|
49 |
+
# Adding JavaScript code to the HTML component
|
50 |
+
gr.HTML(js_code)
|
51 |
|
52 |
+
demo.launch()
|