Skip to content

Commit

Permalink
CELE-18 Add example of async return list function
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Apr 23, 2024
1 parent 5370955 commit 6f345ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
24 changes: 24 additions & 0 deletions applications/visualizer/backend/api/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ninja import NinjaAPI, Router, Schema
from ninja.pagination import paginate, PageNumberPagination
from django.shortcuts import aget_object_or_404
from django.db.models import Q


from .schemas import Dataset, FullDataset, Neuron, Connection, ConnectionRequest
Expand Down Expand Up @@ -74,6 +75,29 @@ async def get_dataset(request, dataset: str):
return await aget_object_or_404(DatasetModel, id=dataset)


@api.get(
"/datasets/{dataset}/neurons",
response={200: list[Neuron], 404: ErrorMessage},
tags=["datasets"],
)
async def get_dataset_neurons(request, dataset: str):
"""Returns all the neurons of a dedicated dataset"""
return await to_list(
NeuronModel.objects.filter(
Q(
name__in=ConnectionModel.objects.filter(
dataset__id=dataset
).values_list("pre", flat=True)
)
| Q(
name__in=ConnectionModel.objects.filter(
dataset__id=dataset
).values_list("post", flat=True)
)
)
)


@api.get("/cells", response=list[Neuron], tags=["neurons"])
@paginate(PageNumberPagination, page_size=50)
def get_all_cells(request):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import {Box, Button, Typography} from "@mui/material";
import { Box, Button, Typography } from "@mui/material";
import { useEffect, useState } from "react";
import { Dataset, DatasetsService, HeathcheckService, Neuron, NeuronsService } from "../rest";

export default function LeftComponent() {
const [ready, setReady] = useState("Not ready")
const [dataset, setDataset] = useState<Dataset>()
const [allDataset, setAllDataset] = useState<Dataset[]>()
const [currentPage, setCurrentPage] = useState<{page: number, loadedElements: number, totalElements: number}>({page: 0, loadedElements: 0, totalElements: 1})
const [currentPage, setCurrentPage] = useState<{ page: number, loadedElements: number, totalElements: number }>({ page: 0, loadedElements: 0, totalElements: 1 })
const [neurons, setNeurons] = useState<Neuron[]>([])

useEffect(() => {
HeathcheckService.ready().then(answer => setReady(answer))
DatasetsService.getDataset({dataset: 'white_1986_whole'}).then(answer => setDataset(answer))
DatasetsService.getDataset({ dataset: 'white_1986_whole' }).then(answer => setDataset(answer))
DatasetsService.getAllDatasets().then(answer => setAllDataset(answer))
loadMoreNeurons()
}, [])

const loadMoreNeurons = async () => {
const page = currentPage.page + 1
const neuronPage = await NeuronsService.getAllCells({page: page})
const neuronPage = await NeuronsService.getAllCells({ page: page })
setNeurons([...neurons, ...neuronPage.items])
setCurrentPage({page: page, loadedElements: neurons.length, totalElements: neuronPage.count})
setCurrentPage({ page: page, loadedElements: neurons.length, totalElements: neuronPage.count })
}

return (
Expand All @@ -37,7 +37,7 @@ export default function LeftComponent() {
<Typography variant="h3">Neurons:</Typography>
<Box>
{
neurons?.map(x => <Typography key={x.name} variant="body1">{x.name}</Typography>)
neurons?.map(x => <Typography key={x.name} variant="body1">{x.name} ({JSON.stringify(x)})</Typography>)
}
<Button disabled={currentPage.loadedElements >= currentPage.totalElements} onClick={loadMoreNeurons}>Load more</Button>
</Box>
Expand Down

0 comments on commit 6f345ff

Please sign in to comment.