Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ on:
repository_dispatch:
types: [notebooks-updated]
jobs:
commit-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: YAML Validity check
uses: ibiqlik/action-yamllint@v3
with:
file_or_dir: .
config_data: |
extends: relaxed
rules:
line-length: disable
trailing-spaces: disable
new-line-at-end-of-file: disable
document-start: disable
indentation: disable
truthy: disable

- name: Run GitLeaks secret scanner
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run Ruff
uses: astral-sh/ruff-action@v3
with:
src: "./src"
args: "format --check"

build:
name: Build Docker Image
runs-on: ubuntu-latest
Expand Down
32 changes: 31 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,39 @@ on:
workflow_dispatch:

jobs:
test:
commit-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: YAML Validity check
uses: ibiqlik/action-yamllint@v3
with:
file_or_dir: .
config_data: |
extends: relaxed
rules:
line-length: disable
trailing-spaces: disable
new-line-at-end-of-file: disable
document-start: disable
indentation: disable
truthy: disable

- name: Run GitLeaks secret scanner
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run Ruff
uses: astral-sh/ruff-action@v3
with:
src: "./src"
args: "format --check"

test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
61 changes: 26 additions & 35 deletions src/generated_api_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import logging
import sys

#Setup logging to console
# Setup logging to console
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler(sys.stdout)
Expand All @@ -38,7 +38,8 @@
logger.info(f"Route path set as: {route_path}")

parameterHandler = NotebookParameters(notebook_path)
PostModel = create_model("post_model",**parameterHandler.dynamic_model)
PostModel = create_model("post_model", **parameterHandler.dynamic_model)


def parse_output(output):
"""
Expand All @@ -47,13 +48,14 @@ def parse_output(output):
match output.get("output_type"):
case "stream":
return handle_streams(output)

case "execute_result" | "display_data":
return handle_rich_outputs(output)

case "error":
return handle_errors(output)


def execute_notebook(notebook, params=None):
logger.info(f"Executing notebook: {notebook}")

Expand All @@ -62,67 +64,56 @@ def execute_notebook(notebook, params=None):
with tempfile.NamedTemporaryFile(suffix=".ipynb") as tmp:
try:
if params is None:
pm.execute_notebook(
notebook,
tmp.name,
kernel_name="global_venv"
)
pm.execute_notebook(notebook, tmp.name, kernel_name="global_venv")
else:
pm.execute_notebook(
notebook,
tmp.name,
params,
kernel_name="global_venv"
)
except PapermillExecutionError as e:
pm.execute_notebook(notebook, tmp.name, params, kernel_name="global_venv")
except PapermillExecutionError as e:
pass

# Read outputs
nb = nbformat.read(tmp.name, as_version=4)

for cell in nb.cells:
if cell.cell_type != "code":
continue
if cell.cell_type != "code":
continue

for output in cell.get("outputs", []):
results.append(parse_output(output))
for output in cell.get("outputs", []):
results.append(parse_output(output))

return results
except ValueError as e:
return f"Error= {e}"


##############
# API Routes #
##############
API_PREFIX = os.environ.get("API_PREFIX", "")
app = FastAPI(docs_url=f"{API_PREFIX}/docs",openapi_url=f"{API_PREFIX}/openapi.json")
app = FastAPI(docs_url=f"{API_PREFIX}/docs", openapi_url=f"{API_PREFIX}/openapi.json")


@app.get(f"{API_PREFIX}/")
async def root():
return RedirectResponse(url=f"{API_PREFIX}{route_path}")


@app.get(f"{API_PREFIX}/getParameters")
async def getParameters():
return parameterHandler.readable_json

@app.get(f"{API_PREFIX}{route_path}",response_model=NotebookResponse)

@app.get(f"{API_PREFIX}{route_path}", response_model=NotebookResponse)
async def endpoint():
output = await asyncio.to_thread(
execute_notebook,
notebook_path,
params=None
)
output = await asyncio.to_thread(execute_notebook, notebook_path, params=None)
return NotebookResponse(outputs=output)

@app.post(f"{API_PREFIX}{route_path}",response_model=NotebookResponse)
async def executeWithParams(params:PostModel):
output = await asyncio.to_thread(
execute_notebook,
notebook_path,
params= params.model_dump()
)

@app.post(f"{API_PREFIX}{route_path}", response_model=NotebookResponse)
async def executeWithParams(params: PostModel):
output = await asyncio.to_thread(execute_notebook, notebook_path, params=params.model_dump())
return NotebookResponse(outputs=output)


@app.get(f"/health")
async def health():
return {"status": "ok"}
return {"status": "ok"}
Loading