broadfield-dev commited on
Commit
a62df18
·
verified ·
1 Parent(s): d02164f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -5,7 +5,7 @@ from io import StringIO
5
 
6
  # Mapping of Gradio components to Toga equivalents
7
  GRADIO_TO_TOGA = {
8
- "Textbox": ("toga.TextInput", "style=Pack(pading=5)", "Text Input"),
9
  "Image": ("toga.Label", "Placeholder: Image file picker", "Image Upload"),
10
  "Button": ("toga.Button", "on_press=button_handler", "Submit"),
11
  "Markdown": ("toga.Label", "style=Pack(padding=5, font_size=14)", "Markdown Text"),
@@ -108,7 +108,7 @@ def generate_toga_code(components, functions):
108
  # Conversion function
109
  def convert_gradio_to_toga(file, code_text):
110
  if not file and not code_text:
111
- return None, "Please upload a Gradio app Python file or paste code in the textbox."
112
 
113
  try:
114
  # Use file if provided, else use text
@@ -116,16 +116,16 @@ def convert_gradio_to_toga(file, code_text):
116
  components, functions = parse_gradio_code(code)
117
 
118
  if not components and functions and isinstance(functions[0], str):
119
- return None, f"Error: {functions[0]}"
120
 
121
  toga_code = generate_toga_code(components, functions)
122
  output_path = "/tmp/toga_app.py"
123
  with open(output_path, "w") as f:
124
  f.write(toga_code)
125
 
126
- return output_path, "Conversion successful! Download the Toga app below."
127
  except Exception as e:
128
- return None, f"Error: {str(e)}"
129
 
130
  # Gradio interface
131
  with gr.Blocks(theme=gr.themes.Soft()) as interface:
@@ -134,13 +134,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as interface:
134
  file_input = gr.File(label="Upload Gradio App", file_types=[".py"])
135
  code_input = gr.Textbox(label="Paste Gradio Code", lines=10, placeholder="Paste your Gradio Python code here...")
136
  convert_button = gr.Button("Convert to Toga")
 
137
  output = gr.File(label="Download Toga App")
138
  status = gr.Textbox(label="Status", interactive=False)
139
 
140
  convert_button.click(
141
  fn=convert_gradio_to_toga,
142
  inputs=[file_input, code_input],
143
- outputs=[output, status]
144
  )
145
 
146
  # Run Gradio for Hugging Face Spaces
 
5
 
6
  # Mapping of Gradio components to Toga equivalents
7
  GRADIO_TO_TOGA = {
8
+ "Textbox": ("toga.TextInput", "style=Pack(padding=5)", "Text Input"),
9
  "Image": ("toga.Label", "Placeholder: Image file picker", "Image Upload"),
10
  "Button": ("toga.Button", "on_press=button_handler", "Submit"),
11
  "Markdown": ("toga.Label", "style=Pack(padding=5, font_size=14)", "Markdown Text"),
 
108
  # Conversion function
109
  def convert_gradio_to_toga(file, code_text):
110
  if not file and not code_text:
111
+ return None, None, "Please upload a Gradio app Python file or paste code in the textbox."
112
 
113
  try:
114
  # Use file if provided, else use text
 
116
  components, functions = parse_gradio_code(code)
117
 
118
  if not components and functions and isinstance(functions[0], str):
119
+ return None, None, f"Error: {functions[0]}"
120
 
121
  toga_code = generate_toga_code(components, functions)
122
  output_path = "/tmp/toga_app.py"
123
  with open(output_path, "w") as f:
124
  f.write(toga_code)
125
 
126
+ return output_path, toga_code, "Conversion successful! Preview the Toga code below and download the app."
127
  except Exception as e:
128
+ return None, None, f"Error: {str(e)}"
129
 
130
  # Gradio interface
131
  with gr.Blocks(theme=gr.themes.Soft()) as interface:
 
134
  file_input = gr.File(label="Upload Gradio App", file_types=[".py"])
135
  code_input = gr.Textbox(label="Paste Gradio Code", lines=10, placeholder="Paste your Gradio Python code here...")
136
  convert_button = gr.Button("Convert to Toga")
137
+ toga_preview = gr.Textbox(label="Toga Code Preview", lines=15, interactive=False)
138
  output = gr.File(label="Download Toga App")
139
  status = gr.Textbox(label="Status", interactive=False)
140
 
141
  convert_button.click(
142
  fn=convert_gradio_to_toga,
143
  inputs=[file_input, code_input],
144
+ outputs=[output, toga_preview, status]
145
  )
146
 
147
  # Run Gradio for Hugging Face Spaces