derek-thomas HF staff commited on
Commit
c2e2a4f
1 Parent(s): d2d7392

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -24
app.py CHANGED
@@ -1,34 +1,52 @@
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 with customized styles
18
- with gr.Blocks(css=".preview-box {border: 1px solid black; padding: 10px;}") 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, className="preview-box")
 
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()
 
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()