derek-thomas HF staff commited on
Commit
f7bea2e
1 Parent(s): 823c3ea

Making it simple and functional

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -1,44 +1,29 @@
1
  import gradio as gr
2
 
3
  # Define the pre-stored text snippets
4
- snippet1 = """|![Link Text](www.example.com)|
5
  |:--:|
6
  |Figure x: Caption|
7
  """
8
- snippet2 = """<div style="background-color: #e6f9e6; padding: 16px 32px; outline: 2px solid; border-radius: 10px;">
9
  This is text with a tip format!
10
  </div>
11
  """
12
- snippet3 = """[[1]](#1)
13
-
14
- <a id="1">[1]</a> : First Last, [Example](www.example.com), 2021"""
15
-
16
- # JavaScript to get and set cursor position
17
- js_code = """
18
- <script>
19
- let position = 0;
20
- function updatePosition() {
21
- let textbox = document.querySelector('textarea');
22
- position = textbox.selectionStart;
23
- }
24
- function insertAtCursor(snippet) {
25
- let textbox = document.querySelector('textarea');
26
- let text = textbox.value;
27
- let newText = text.slice(0, position) + snippet + text.slice(position);
28
- textbox.value = newText;
29
- position += snippet.length;
30
- textbox.focus();
31
- // Triggering input event to update Gradio component
32
- let event = new Event('input', { bubbles: true });
33
- textbox.dispatchEvent(event);
34
- }
35
- document.querySelector('textarea').addEventListener('click', updatePosition);
36
- document.querySelector('textarea').addEventListener('keyup', updatePosition);
37
- document.querySelector('#button1').addEventListener('click', function() { insertAtCursor(`|![Link Text](www.example.com)|\n|:--:|\n|Figure x: Caption|\n`); });
38
- document.querySelector('#button2').addEventListener('click', function() { insertAtCursor(`<div style="background-color: #e6f9e6; padding: 16px 32px; outline: 2px solid; border-radius: 10px;">\nThis is text with a tip format!\n</div>\n`); });
39
- document.querySelector('#button3').addEventListener('click', function() { insertAtCursor(`<a id="1">[1]</a> : First Last, [Example](www.example.com), 2021\n`); });
40
- </script>
41
- """
42
 
43
  with gr.Blocks() as demo:
44
  gr.Markdown("# Blog Writing Helper")
@@ -53,8 +38,13 @@ with gr.Blocks() as demo:
53
  gr.Markdown("**Live Preview**")
54
  markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")
55
 
 
 
 
 
 
56
  # Live preview update
57
- text_input.change(lambda text: text, inputs=text_input, outputs=markdown_preview)
58
 
59
  # Adding JavaScript code to the HTML component
60
  gr.HTML(js_code)
 
1
  import gradio as gr
2
 
3
  # Define the pre-stored text snippets
4
+ snippet1 = """\n\n|![Link Text](www.example.com)|
5
  |:--:|
6
  |Figure x: Caption|
7
  """
8
+ snippet2 = """\n\n<div style="background-color: #e6f9e6; padding: 16px 32px; outline: 2px solid; border-radius: 10px;">
9
  This is text with a tip format!
10
  </div>
11
  """
12
+ snippet3 = """\n\n[[1]](#1)\n\n<a id="1">[1]</a> : First Last, [Example](www.example.com), 2021"""
13
+
14
+ # Functions to append snippets at the end of the text
15
+ def append_snippet1(text):
16
+ return text + snippet1
17
+
18
+ def append_snippet2(text):
19
+ return text + snippet2
20
+
21
+ def append_snippet3(text):
22
+ return text + snippet3
23
+
24
+ # Function to update live preview
25
+ def preview_text(text):
26
+ return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  with gr.Blocks() as demo:
29
  gr.Markdown("# Blog Writing Helper")
 
38
  gr.Markdown("**Live Preview**")
39
  markdown_preview = gr.Markdown(label="Live Preview", elem_id="preview-box")
40
 
41
+ # Button click actions to append snippets
42
+ button1.click(append_snippet1, inputs=text_input, outputs=text_input)
43
+ button2.click(append_snippet2, inputs=text_input, outputs=text_input)
44
+ button3.click(append_snippet3, inputs=text_input, outputs=text_input)
45
+
46
  # Live preview update
47
+ text_input.change(preview_text, inputs=text_input, outputs=markdown_preview)
48
 
49
  # Adding JavaScript code to the HTML component
50
  gr.HTML(js_code)