derek-thomas HF staff commited on
Commit
766bba1
1 Parent(s): 4f2747f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -50
app.py CHANGED
@@ -1,59 +1,37 @@
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
- with gr.Blocks() as demo:
12
- gr.Markdown("# Blog Writing Helper")
13
-
 
 
 
 
 
 
 
14
  with gr.Row():
15
  with gr.Column():
16
- text_input = gr.Textbox(lines=10, placeholder="Write your blog here...", label="Text Editor")
17
- button1 = gr.Button("Insert Snippet 1")
18
- button2 = gr.Button("Insert Snippet 2")
 
19
  with gr.Column():
20
- gr.Markdown("**Live Preview**")
21
- markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")
22
-
23
- button1.click(lambda text, position: insert_snippet(text, snippet1, position), inputs=[text_input, gr.State(0)], outputs=text_input)
24
- button2.click(lambda text, position: insert_snippet(text, snippet2, position), inputs=[text_input, gr.State(0)], outputs=text_input)
25
- text_input.change(lambda text: text, inputs=text_input, outputs=markdown_preview)
26
-
27
- # Add custom JavaScript to get cursor position
28
- js_code = """
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)