fastpyrepl 0.1.0__tar.gz
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-0.1.0/.gitignore +207 -0
- fastpyrepl-0.1.0/Dockerfile +2 -0
- fastpyrepl-0.1.0/LICENSE +21 -0
- fastpyrepl-0.1.0/PKG-INFO +21 -0
- fastpyrepl-0.1.0/README.md +2 -0
- fastpyrepl-0.1.0/pyproject.toml +33 -0
- fastpyrepl-0.1.0/src/fastpyrepl/__init__.py +0 -0
- fastpyrepl-0.1.0/src/fastpyrepl/main.py +77 -0
- fastpyrepl-0.1.0/src/fastpyrepl/server_utils.py +62 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
fastpyrepl-0.1.0/LICENSE
ADDED
|
@@ -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.
|
|
@@ -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,33 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastpyrepl"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "FastAPI service for executing Python code in session-scoped environments"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Jyotin Goel", email = "b22ai063@iitj.ac.in" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"fastapi>=0.115.0",
|
|
12
|
+
"pydantic>=2.0.0",
|
|
13
|
+
"uvicorn>=0.30.0",
|
|
14
|
+
"typer>=0.12.0",
|
|
15
|
+
]
|
|
16
|
+
license = { text = "MIT" }
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Framework :: FastAPI",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.scripts]
|
|
26
|
+
fastpyrepl = "fastpyrepl.main:main"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["hatchling"]
|
|
30
|
+
build-backend = "hatchling.build"
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.wheel]
|
|
33
|
+
packages = ["src/fastpyrepl"]
|
|
File without changes
|
|
@@ -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)
|