gstaff commited on
Commit
7563e81
·
1 Parent(s): a8e9c78

Fix escaping on snippet export.

Browse files
Files changed (1) hide show
  1. templates.py +6 -6
templates.py CHANGED
@@ -60,7 +60,7 @@ def load_js(demo_type: DemoType) -> str:
60
 
61
  let template = `{gradio_lite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`;
62
  if (codeValue) {{
63
- template = `{gradio_lite_html_template}`.replace('STARTING_CODE', codeValue.replace(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replace('`', String.fromCharCode(92) + '`'));
64
  }}
65
  template = template.replace('STARTING_REQUIREMENTS', requirementsValue || '');
66
  const frame = document.getElementById('gradio-iframe');
@@ -182,17 +182,17 @@ def copy_share_link_js(demo_type: DemoType) -> str:
182
  def copy_snippet_js(demo_type: DemoType) -> str:
183
  if demo_type == DemoType.GRADIO:
184
  return f"""async (code, requirements) => {{
185
- const escapedCode = code.replace(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replace('`', String.fromCharCode(92) + '`');
186
  const template = `{gradio_lite_snippet_template}`;
187
  // Step 1: Generate the HTML content
188
- const completedTemplate = template.replace('STARTING_CODE', code).replace('STARTING_REQUIREMENTS', requirements);
189
  const snippet = completedTemplate;
190
  await navigator.clipboard.writeText(snippet);
191
  return [code, requirements];
192
  }}"""
193
  elif demo_type == DemoType.STREAMLIT:
194
  return f"""async (code) => {{
195
- const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
196
  const template = `{stlite_snippet_template}`;
197
  // Step 1: Generate the HTML content
198
  const completedTemplate = template.replace('STARTING_CODE', code);
@@ -206,7 +206,7 @@ def copy_snippet_js(demo_type: DemoType) -> str:
206
  def download_code_js(demo_type: DemoType) -> str:
207
  if demo_type == demo_type.GRADIO:
208
  return f"""(code, requirements) => {{
209
- const escapedCode = code.replace(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replace('`', String.fromCharCode(92) + '`');
210
  // Step 1: Generate the HTML content
211
  const completedTemplate = `{gradio_lite_html_template}`.replace('STARTING_CODE', escapedCode).replace('STARTING_REQUIREMENTS', requirements);
212
 
@@ -229,7 +229,7 @@ def download_code_js(demo_type: DemoType) -> str:
229
  }}"""
230
  elif demo_type == demo_type.STREAMLIT:
231
  return f"""(code) => {{
232
- const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
233
  // Step 1: Generate the HTML content
234
  const completedTemplate = `{stlite_html_template}`.replace('STARTING_CODE', escapedCode);
235
 
 
60
 
61
  let template = `{gradio_lite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`;
62
  if (codeValue) {{
63
+ template = `{gradio_lite_html_template}`.replace('STARTING_CODE', codeValue.replaceAll(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replaceAll('`', String.fromCharCode(92) + '`'));
64
  }}
65
  template = template.replace('STARTING_REQUIREMENTS', requirementsValue || '');
66
  const frame = document.getElementById('gradio-iframe');
 
182
  def copy_snippet_js(demo_type: DemoType) -> str:
183
  if demo_type == DemoType.GRADIO:
184
  return f"""async (code, requirements) => {{
185
+ const escapedCode = code.replaceAll(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92) + String.fromCharCode(92) + String.fromCharCode(92)).replaceAll('`', String.fromCharCode(92) + '`');
186
  const template = `{gradio_lite_snippet_template}`;
187
  // Step 1: Generate the HTML content
188
+ const completedTemplate = template.replace('STARTING_CODE', escapedCode).replace('STARTING_REQUIREMENTS', requirements);
189
  const snippet = completedTemplate;
190
  await navigator.clipboard.writeText(snippet);
191
  return [code, requirements];
192
  }}"""
193
  elif demo_type == DemoType.STREAMLIT:
194
  return f"""async (code) => {{
195
+ const escapedCode = code.replaceAll(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92) + String.fromCharCode(92)).replaceAll('`', String.fromCharCode(92) + '`');
196
  const template = `{stlite_snippet_template}`;
197
  // Step 1: Generate the HTML content
198
  const completedTemplate = template.replace('STARTING_CODE', code);
 
206
  def download_code_js(demo_type: DemoType) -> str:
207
  if demo_type == demo_type.GRADIO:
208
  return f"""(code, requirements) => {{
209
+ const escapedCode = code.replaceAll(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replaceAll('`', String.fromCharCode(92) + '`');
210
  // Step 1: Generate the HTML content
211
  const completedTemplate = `{gradio_lite_html_template}`.replace('STARTING_CODE', escapedCode).replace('STARTING_REQUIREMENTS', requirements);
212
 
 
229
  }}"""
230
  elif demo_type == demo_type.STREAMLIT:
231
  return f"""(code) => {{
232
+ const escapedCode = code.replaceAll(String.fromCharCode(92), String.fromCharCode(92) + String.fromCharCode(92)).replaceAll('`', String.fromCharCode(92) + '`');
233
  // Step 1: Generate the HTML content
234
  const completedTemplate = `{stlite_html_template}`.replace('STARTING_CODE', escapedCode);
235