Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
snippet1 = "
|
| 5 |
-
snippet2 = "
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return text[:position] + snippet + text[position:]
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with gr.Row():
|
| 15 |
with gr.Column():
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
with gr.Column():
|
| 20 |
-
gr.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
<script>
|
| 30 |
-
let textbox = document.querySelector('textarea');
|
| 31 |
-
let position = 0;
|
| 32 |
-
|
| 33 |
-
textbox.addEventListener('click', () => {
|
| 34 |
-
position = textbox.selectionStart;
|
| 35 |
-
});
|
| 36 |
-
|
| 37 |
-
textbox.addEventListener('keyup', () => {
|
| 38 |
-
position = textbox.selectionStart;
|
| 39 |
-
});
|
| 40 |
-
|
| 41 |
-
gradio.input('state', position);
|
| 42 |
-
</script>
|
| 43 |
-
"""
|
| 44 |
-
gr.HTML(js_code)
|
| 45 |
-
|
| 46 |
-
# Add custom CSS for the preview box
|
| 47 |
-
css = """
|
| 48 |
-
#preview-box {
|
| 49 |
-
border: 1px solid #ccc;
|
| 50 |
-
padding: 10px;
|
| 51 |
-
height: auto;
|
| 52 |
-
min-height: 200px;
|
| 53 |
-
max-height: 400px;
|
| 54 |
-
overflow: auto;
|
| 55 |
-
background-color: #f9f9f9;
|
| 56 |
-
}
|
| 57 |
-
"""
|
| 58 |
|
| 59 |
demo.launch(css=css)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Predefined snippets
|
| 4 |
+
snippet1 = "Here's a helpful tip for your blog post: "
|
| 5 |
+
snippet2 = "Conclusion: Always remember to summarize your key points."
|
| 6 |
|
| 7 |
+
def update_preview(text):
|
| 8 |
+
return text
|
|
|
|
| 9 |
|
| 10 |
+
def insert_snippet(text, editor_content, cursor_position):
|
| 11 |
+
# Insert the snippet at the current cursor position
|
| 12 |
+
new_content = (
|
| 13 |
+
editor_content[:cursor_position] + text + editor_content[cursor_position:]
|
| 14 |
+
)
|
| 15 |
+
return new_content, new_content, cursor_position + len(text)
|
| 16 |
+
|
| 17 |
+
# Create the interface
|
| 18 |
+
with gr.Blocks() as app:
|
| 19 |
+
gr.Markdown("### Blog Editor with Live Preview")
|
| 20 |
with gr.Row():
|
| 21 |
with gr.Column():
|
| 22 |
+
text_editor = gr.TextArea(label="Write your blog here", placeholder="Start typing...", lines=10, elem_id="editor")
|
| 23 |
+
with gr.Row():
|
| 24 |
+
insert_button1 = gr.Button("Insert Snippet 1")
|
| 25 |
+
insert_button2 = gr.Button("Insert Snippet 2")
|
| 26 |
with gr.Column():
|
| 27 |
+
preview_box = gr.HTML(label="Live Preview", elem_id="preview", visible=True, style={'border': '1px solid black', 'padding': '10px'})
|
| 28 |
+
|
| 29 |
+
# Interactivity
|
| 30 |
+
text_editor.change(fn=update_preview, inputs=text_editor, outputs=preview_box)
|
| 31 |
+
insert_button1.click(fn=insert_snippet, inputs=[snippet1, text_editor, text_editor.cursor_position], outputs=[text_editor, preview_box, text_editor.cursor_position])
|
| 32 |
+
insert_button2.click(fn=insert_snippet, inputs=[snippet2, text_editor, text_editor.cursor_position], outputs=[text_editor, preview_box, text_editor.cursor_position])
|
| 33 |
+
|
| 34 |
+
app.launch()
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
demo.launch(css=css)
|