fastpyrepl 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastpyrepl/__init__.py +0 -0
- fastpyrepl/main.py +77 -0
- fastpyrepl/server_utils.py +62 -0
- fastpyrepl-0.1.0.dist-info/METADATA +21 -0
- fastpyrepl-0.1.0.dist-info/RECORD +8 -0
- fastpyrepl-0.1.0.dist-info/WHEEL +4 -0
- fastpyrepl-0.1.0.dist-info/entry_points.txt +2 -0
- fastpyrepl-0.1.0.dist-info/licenses/LICENSE +21 -0
fastpyrepl/__init__.py
ADDED
|
File without changes
|
fastpyrepl/main.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from typing import Annotated
|
|
2
|
+
from threading import Lock
|
|
3
|
+
from uuid import uuid4
|
|
4
|
+
from fastapi import FastAPI
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
import typer
|
|
7
|
+
import uvicorn
|
|
8
|
+
from fastpyrepl.server_utils import ExecRequest, ExecResponse, SessionEnv
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
app = FastAPI(title="Python HTTP Executor")
|
|
12
|
+
_sessions: dict[str, SessionEnv] = {}
|
|
13
|
+
_sessions_lock = Lock()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ResetRequest(BaseModel):
|
|
17
|
+
session_id: str | None = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@app.get("/health")
|
|
21
|
+
def health() -> dict[str, str]:
|
|
22
|
+
return {"status": "PYTHON REPL ACTIVE"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@app.post("/execute", response_model=ExecResponse)
|
|
26
|
+
def execute(req: ExecRequest) -> ExecResponse:
|
|
27
|
+
session_id = req.session_id or str(uuid4())
|
|
28
|
+
|
|
29
|
+
with _sessions_lock:
|
|
30
|
+
env = _sessions.get(session_id)
|
|
31
|
+
if env is None:
|
|
32
|
+
env = SessionEnv(context=req.context, setup_code=req.setup_code)
|
|
33
|
+
_sessions[session_id] = env
|
|
34
|
+
elif req.context is not None:
|
|
35
|
+
env.locals["context"] = req.context
|
|
36
|
+
|
|
37
|
+
stdout, stderr, error, execution_time = env.execute(req.code)
|
|
38
|
+
local_values = None
|
|
39
|
+
if req.return_locals:
|
|
40
|
+
if req.locals_keys:
|
|
41
|
+
local_values = {key: env.locals.get(key) for key in req.locals_keys}
|
|
42
|
+
else:
|
|
43
|
+
local_values = dict(env.locals)
|
|
44
|
+
|
|
45
|
+
return ExecResponse(
|
|
46
|
+
stdout=stdout,
|
|
47
|
+
stderr=stderr,
|
|
48
|
+
locals=local_values,
|
|
49
|
+
execution_time=execution_time,
|
|
50
|
+
session_id=session_id,
|
|
51
|
+
error=error,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@app.post("/reset")
|
|
56
|
+
def reset(req: ResetRequest) -> dict[str, str]:
|
|
57
|
+
with _sessions_lock:
|
|
58
|
+
if req.session_id:
|
|
59
|
+
_sessions.pop(req.session_id, None)
|
|
60
|
+
return {"status": "ok", "message": f"reset session {req.session_id}"}
|
|
61
|
+
_sessions.clear()
|
|
62
|
+
return {"status": "ok", "message": "reset all sessions"}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def main() -> None:
|
|
66
|
+
def run(
|
|
67
|
+
host: Annotated[str, typer.Option(help="Host interface to bind")] = "0.0.0.0",
|
|
68
|
+
port: Annotated[int, typer.Option(help="Port to listen on")] = 8000,
|
|
69
|
+
reload: Annotated[bool, typer.Option(help="Enable auto-reload")] = False,
|
|
70
|
+
) -> None:
|
|
71
|
+
uvicorn.run("fastpyrepl.main:app", host=host, port=port, reload=reload)
|
|
72
|
+
|
|
73
|
+
typer.run(run)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if __name__ == "__main__":
|
|
77
|
+
main()
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
from typing import Any, List, Dict, Optional
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
class ExecRequest(BaseModel):
|
|
6
|
+
code: str
|
|
7
|
+
context: Optional[str] = None
|
|
8
|
+
setup_code: Optional[str] = None
|
|
9
|
+
session_id: Optional[str] = None
|
|
10
|
+
return_locals: bool = False
|
|
11
|
+
locals_keys: Optional[List[str]] = None
|
|
12
|
+
|
|
13
|
+
class ExecResponse(BaseModel):
|
|
14
|
+
stdout: str
|
|
15
|
+
stderr: str
|
|
16
|
+
locals: Optional[Dict[str, Any]] = None
|
|
17
|
+
execution_time: float
|
|
18
|
+
session_id: str
|
|
19
|
+
error: Optional[str] = None
|
|
20
|
+
|
|
21
|
+
class SessionEnv:
|
|
22
|
+
def __init__(self, context=None, setup_code=None) -> None:
|
|
23
|
+
self.globals = {'__builtins__': __builtins__}
|
|
24
|
+
self.locals = {}
|
|
25
|
+
if context is not None:
|
|
26
|
+
self.locals["context"] = context
|
|
27
|
+
if setup_code:
|
|
28
|
+
self.execute(setup_code)
|
|
29
|
+
|
|
30
|
+
def execute(self, code: str):
|
|
31
|
+
import io, sys
|
|
32
|
+
start = time.time()
|
|
33
|
+
stdout = io.StringIO()
|
|
34
|
+
stderr = io.StringIO()
|
|
35
|
+
old_stdout, old_stderr = sys.stdout, sys.stderr
|
|
36
|
+
try:
|
|
37
|
+
sys.stdout, sys.stderr = stdout, stderr
|
|
38
|
+
exec(code, self.globals, self.locals)
|
|
39
|
+
return stdout.getvalue(), stderr.getvalue(), None, time.time() - start
|
|
40
|
+
except Exception as e:
|
|
41
|
+
return stdout.getvalue(), stderr.getvalue() + str(e), str(e), time.time() - start
|
|
42
|
+
finally:
|
|
43
|
+
sys.stdout, sys.stderr = old_stdout, old_stderr
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# if __name__ == "__main__":
|
|
47
|
+
# test_code = """import numpy as np
|
|
48
|
+
# a = np.array([1, 2, 3, 4, 5], dtype=float)
|
|
49
|
+
# b = np.array([5, 4, 3, 2, 1], dtype=float)
|
|
50
|
+
# print("a + b:", a + b)
|
|
51
|
+
# print("a * b:", a * b)
|
|
52
|
+
# print("mean(a):", np.mean(a))
|
|
53
|
+
# m = np.array([[1, 2], [3, 4]], dtype=float)
|
|
54
|
+
# print("matrix:\\n", m)
|
|
55
|
+
# print("transpose:\\n", m.T)
|
|
56
|
+
# print("determinant:", np.linalg.det(m))"""
|
|
57
|
+
# env = SessionEnv(
|
|
58
|
+
# context="",
|
|
59
|
+
# )
|
|
60
|
+
# stdout, stderr, error, execution_time = env.execute(test_code)
|
|
61
|
+
# print(stderr)
|
|
62
|
+
# print(env.locals)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastpyrepl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: FastAPI service for executing Python code in session-scoped environments
|
|
5
|
+
Author-email: Jyotin Goel <b22ai063@iitj.ac.in>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Framework :: FastAPI
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Requires-Dist: fastapi>=0.115.0
|
|
15
|
+
Requires-Dist: pydantic>=2.0.0
|
|
16
|
+
Requires-Dist: typer>=0.12.0
|
|
17
|
+
Requires-Dist: uvicorn>=0.30.0
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# docker-python-repl
|
|
21
|
+
Docker Python REPL for execution of code over HTTP
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
fastpyrepl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
fastpyrepl/main.py,sha256=gNuhiTwrBJohX-WYd7jCqeRkjmyJxM_03V1PxooEOCA,2215
|
|
3
|
+
fastpyrepl/server_utils.py,sha256=gnzXFzqsMAhDr813Eb-L6xjnc7m2qQH3PmWNtrC8gpM,1965
|
|
4
|
+
fastpyrepl-0.1.0.dist-info/METADATA,sha256=e-mCSnwTKYoQnIryt__lD1H270-NdhMvNphcY9s2Cjg,714
|
|
5
|
+
fastpyrepl-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
fastpyrepl-0.1.0.dist-info/entry_points.txt,sha256=0WDbAwZiBTHrZS45qGUDgFxm6jTy02irBc62mMKYFF0,52
|
|
7
|
+
fastpyrepl-0.1.0.dist-info/licenses/LICENSE,sha256=rDOfESyzpQ5Na0KatMA7BPVJzQSP0QAjmpP2EFud1UA,1068
|
|
8
|
+
fastpyrepl-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jyotin Goel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|