|
import gradio as gr |
|
import requests |
|
from bs4 import BeautifulSoup |
|
|
|
DEF_SNIPPET = "print('Hello, World!')" |
|
DEF_LANG = "Python" |
|
|
|
def execute_snippet(code_snippet: str = DEF_SNIPPET, lang: str = DEF_LANG) -> str: |
|
lang_param = None |
|
|
|
match lang: |
|
case "C++": lang_param = "cpp" |
|
case "C#": lang_param = "cs" |
|
case _: lang_param = lang.lower() |
|
|
|
|
|
res = requests.request("POST", f"https://try.w3schools.com/try_{lang_param}.php", data={ |
|
"code": code_snippet |
|
}) |
|
match lang_param: |
|
case "php": return res.text |
|
case _: return BeautifulSoup(res.text, "html.parser").find_all("pre")[0].string |
|
|
|
demo = gr.Interface( |
|
fn=execute_snippet, |
|
inputs=[ |
|
gr.Textbox( |
|
show_label=True, |
|
label="Code", |
|
max_lines=4_294_967_295, |
|
lines=4_294_967_295, |
|
value=DEF_SNIPPET, |
|
), |
|
gr.Dropdown( |
|
show_label=True, |
|
label="Language", |
|
choices=["Python", "Java", "C", "C++", "C#", "PHP"], |
|
value=DEF_LANG |
|
), |
|
], |
|
outputs=gr.Textbox(label="Result"), |
|
title="HFChat Code Executor", |
|
description="Enter the code snippet and language that you want to execute.", |
|
) |
|
|
|
demo.launch() |