Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
|
| 4 |
-
|
| 5 |
-
def upload_file(filepath):
|
| 6 |
-
name = Path(filepath).name
|
| 7 |
-
return [gr.UploadButton(visible=True), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
|
| 8 |
-
|
| 9 |
-
def download_file():
|
| 10 |
-
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=True)]
|
| 11 |
def sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, replacement, text):
|
| 12 |
import re
|
| 13 |
class emptymatch:
|
|
@@ -17,8 +10,6 @@ def sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, r
|
|
| 17 |
|
| 18 |
matches1 = list(re.finditer(fullpattern, text))
|
| 19 |
matches2 = list(re.finditer(excludedsubpattern, text))
|
| 20 |
-
# print("Matches 1 and positions:", [(match.group(), match.start(), match.end(), match) for match in matches1])
|
| 21 |
-
# print("Matches 2 and positions:", [(match.group(), match.start(), match.end(), match) for match in matches2])
|
| 22 |
|
| 23 |
matches1.extend([emptymatch()]*(len(matches2)-len(matches1)))
|
| 24 |
matches2.extend([emptymatch()]*(len(matches1)-len(matches2)))
|
|
@@ -27,9 +18,6 @@ def sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, r
|
|
| 27 |
for match1, match2 in zip(matches1, matches2):
|
| 28 |
if match1.start()>=0 and match1.start() == match2.start() and match1.end() == match2.end():
|
| 29 |
matches.append(match1)
|
| 30 |
-
# print("Common Matches and positions:", [(match.group(), match.start(), match.end(), match) for match in matches])
|
| 31 |
-
|
| 32 |
-
# print("Matches and positions:", [(match.group(), match.start(), match.end()) for match in matches])
|
| 33 |
|
| 34 |
modified_text = text
|
| 35 |
|
|
@@ -63,15 +51,12 @@ def Input(prompt=""):
|
|
| 63 |
"""+modified_code
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
# Create a temporary file for the user's code
|
| 67 |
with open(filename.removesuffix('.py')+'.py','wb') as file:
|
| 68 |
file.write(modified_code.encode('utf-8'))
|
| 69 |
file_path = file.name
|
| 70 |
|
| 71 |
-
# Prepare inputs
|
| 72 |
input_data = inputs.replace("\\n", "\n")
|
| 73 |
|
| 74 |
-
# Execute the Python script with the provided inputs
|
| 75 |
process = subprocess.run(
|
| 76 |
["python", file_path],
|
| 77 |
input=input_data,
|
|
@@ -79,7 +64,6 @@ def Input(prompt=""):
|
|
| 79 |
capture_output=True
|
| 80 |
)
|
| 81 |
|
| 82 |
-
# Collect output and errors
|
| 83 |
output = ''.join(echo)+process.stdout
|
| 84 |
errors = process.stderr
|
| 85 |
|
|
@@ -99,9 +83,7 @@ description = (
|
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
-
with gr.Blocks() as demo:
|
| 103 |
-
# Add custom CSS for black background and white monospace text for the output box
|
| 104 |
-
|
| 105 |
gr.Interface(
|
| 106 |
fn=run_code_with_inputs,
|
| 107 |
inputs=[
|
|
@@ -116,19 +98,10 @@ with gr.Blocks() as demo:
|
|
| 116 |
),
|
| 117 |
title="Python Console Runner",
|
| 118 |
description=description,
|
| 119 |
-
submit_btn=">_ Run"
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
# current_file = ['my_program.py']
|
| 124 |
-
# with gr.Row():
|
| 125 |
-
# t = gr.Textbox(label="File Name", lines=1, placeholder="Enter file name...", value="my_program.py", interactive=True)
|
| 126 |
-
# def get_file():
|
| 127 |
-
# subprocess.run(["mv", current_file[0], t.value], check=False)
|
| 128 |
-
# current_file[0] = t.value
|
| 129 |
-
# return f"/{current_file[0]}"
|
| 130 |
-
# d = gr.DownloadButton(label="Download .py file", value=get_file, visible=True)
|
| 131 |
-
|
| 132 |
demo.css = """
|
| 133 |
#output-box {
|
| 134 |
background-color: black;
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def sub_pattern_with_exclusion(fullpattern, excludedsubpattern, targetpattern, replacement, text):
|
| 5 |
import re
|
| 6 |
class emptymatch:
|
|
|
|
| 10 |
|
| 11 |
matches1 = list(re.finditer(fullpattern, text))
|
| 12 |
matches2 = list(re.finditer(excludedsubpattern, text))
|
|
|
|
|
|
|
| 13 |
|
| 14 |
matches1.extend([emptymatch()]*(len(matches2)-len(matches1)))
|
| 15 |
matches2.extend([emptymatch()]*(len(matches1)-len(matches2)))
|
|
|
|
| 18 |
for match1, match2 in zip(matches1, matches2):
|
| 19 |
if match1.start()>=0 and match1.start() == match2.start() and match1.end() == match2.end():
|
| 20 |
matches.append(match1)
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
modified_text = text
|
| 23 |
|
|
|
|
| 51 |
"""+modified_code
|
| 52 |
|
| 53 |
try:
|
|
|
|
| 54 |
with open(filename.removesuffix('.py')+'.py','wb') as file:
|
| 55 |
file.write(modified_code.encode('utf-8'))
|
| 56 |
file_path = file.name
|
| 57 |
|
|
|
|
| 58 |
input_data = inputs.replace("\\n", "\n")
|
| 59 |
|
|
|
|
| 60 |
process = subprocess.run(
|
| 61 |
["python", file_path],
|
| 62 |
input=input_data,
|
|
|
|
| 64 |
capture_output=True
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
output = ''.join(echo)+process.stdout
|
| 68 |
errors = process.stderr
|
| 69 |
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
+
with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as demo:
|
|
|
|
|
|
|
| 87 |
gr.Interface(
|
| 88 |
fn=run_code_with_inputs,
|
| 89 |
inputs=[
|
|
|
|
| 98 |
),
|
| 99 |
title="Python Console Runner",
|
| 100 |
description=description,
|
| 101 |
+
submit_btn=">_ Run",
|
| 102 |
+
clear_btn=None
|
| 103 |
)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
demo.css = """
|
| 106 |
#output-box {
|
| 107 |
background-color: black;
|