Skip to content

Commit

Permalink
Merge pull request #5 from Zero-True/notebook-toml
Browse files Browse the repository at this point in the history
Load and maintain project toml
  • Loading branch information
Red-Giuliano authored Sep 13, 2023
2 parents 4f0c90a + 689b1d4 commit e43b5cf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

#Custom stuff
zt_backend/notebook.toml
18 changes: 18 additions & 0 deletions zt_backend/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from typing import OrderedDict
from zt_backend.models.notebook import Notebook, CodeCell
import router
import toml
import os
import uuid

app = FastAPI()

Expand All @@ -24,6 +28,20 @@
expose_headers=["*"]
)

@app.on_event("startup")
def open_project():
if not os.path.exists('notebook.toml'):
codeCell = CodeCell(
id=str(uuid.uuid4()),
code='#code here or else',
components=[],
output='',
cellType='code'
)
zt_notebook = Notebook(cells=OrderedDict([(codeCell.id, codeCell)]))
with open('notebook.toml', "w") as project_file:
toml.dump(zt_notebook.model_dump(), project_file)

if run_mode=='app':
app.mount(route_prefix, StaticFiles(directory="dist_app"), name="assets")
else:
Expand Down
3 changes: 2 additions & 1 deletion zt_backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fastapi
uvicorn
pydantic
pydantic
toml
42 changes: 35 additions & 7 deletions zt_backend/router.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from fastapi import APIRouter, Request
from fastapi import APIRouter
from fastapi.responses import FileResponse
from zt_backend.models import request
from zt_backend.models import notebook
from zt_backend.models import request, notebook, response
from runner.execute_code import execute_request
import os
import uuid
import toml

router = APIRouter()

Expand All @@ -20,15 +20,43 @@ def health():
return('UP')

@router.post("/api/runcode")
async def runcode(request: request.Request):
return execute_request(request)
def runcode(request: request.Request):
globalStateUpdate(run_request=request)
response = execute_request(request)
globalStateUpdate(run_response=response)
return response

@router.post("/api/create_cell")
def create_cell():
return notebook.CodeCell(
createdCell = notebook.CodeCell(
id=str(uuid.uuid4()),
code='#code here or else',
components=[],
output='',
cellType='code'
)
)
globalStateUpdate(newCell=createdCell)
return createdCell

@router.get("/api/notebook")
def get_notebook():
return get_notebook()

def get_notebook():
with open('notebook.toml', "r") as project_file:
notebook_data = toml.load(project_file)
return notebook.Notebook(**notebook_data)

def globalStateUpdate(newCell: notebook.CodeCell=None, run_request: request.Request=None, run_response: response.Response=None):
zt_notebook = get_notebook()
if newCell is not None:
zt_notebook.cells[newCell.id] = newCell
if run_request is not None:
for requestCell in run_request.cells:
zt_notebook.cells[requestCell.id].code = requestCell.code
if run_response is not None:
for responseCell in run_response.cells:
zt_notebook.cells[responseCell.id].components = responseCell.components
zt_notebook.cells[responseCell.id].output = responseCell.output
with open('notebook.toml', "w") as project_file:
toml.dump(zt_notebook.model_dump(), project_file)
18 changes: 8 additions & 10 deletions zt_frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,22 @@ export default {
},
async created() {
const response = await axios.post(import.meta.env.VITE_BACKEND_URL + 'api/create_cell');
const startCell: CodeCell = response.data
this.notebook.cells = {}
this.notebook.cells[startCell.id] = startCell
const response = await axios.get(import.meta.env.VITE_BACKEND_URL + 'api/notebook')
this.notebook = response.data
},
methods: {
async runCode() {
const cellRequests: CodeRequest[] = [];
const cellRequests: CodeRequest[] = []
const requestComponents: ZTComponent[] = []
for (let key in this.notebook.cells){
const cellRequest: CodeRequest = {id: key, code: this.notebook.cells[key].code};
const cellRequest: CodeRequest = {id: key, code: this.notebook.cells[key].code}
requestComponents.push.apply(requestComponents, this.notebook.cells[key].components)
cellRequests.push(cellRequest);
cellRequests.push(cellRequest)
}
const request: Request = { cells: cellRequests, components: requestComponents };
const axiosResponse = await axios.post(import.meta.env.VITE_BACKEND_URL + 'api/runcode', request);
const response: Response = axiosResponse.data;
const request: Request = { cells: cellRequests, components: requestComponents }
const axiosResponse = await axios.post(import.meta.env.VITE_BACKEND_URL + 'api/runcode', request)
const response: Response = axiosResponse.data
for (const cellResponse of response.cells){
this.notebook.cells[cellResponse.id].components = cellResponse.components
this.notebook.cells[cellResponse.id].output = cellResponse.output
Expand Down

0 comments on commit e43b5cf

Please sign in to comment.