Skip to content

Commit

Permalink
Add scan JSON and port selection to App
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-neurosc committed Nov 27, 2024
1 parent d8581c1 commit f8375c5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions gui_dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"prettier": "^3.3.3",
"react-scan": "^0.0.31",
"vite": "^5.4.11"
}
}
11 changes: 11 additions & 0 deletions gui_dev/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
if (JSON.parse(import.meta.env.VITE_REACT_SCAN) === true) {
import("react-scan").then(({ scan }) => {
scan({
enabled: true,
log: true, // logs render info to console
});
});
}

import { StrictMode } from "react";
import ReactDOM from "react-dom/client";
import { App } from "./App.jsx";

// Set up react-scan

// Ignore React 19 warning about accessing element.ref
const originalConsoleError = console.error;
console.error = (message, ...messageArgs) => {
Expand Down
1 change: 0 additions & 1 deletion gui_dev/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default defineConfig(() => {
},
},
server: {
port: 54321,
proxy: {
"/api": {
target: `http://localhost:${BACKEND_PORT}`,
Expand Down
33 changes: 29 additions & 4 deletions py_neuromodulation/gui/backend/app_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# Shared memory configuration
ARRAY_SIZE = 1000 # Adjust based on your needs

SERVER_PORT = 50001
DEV_SERVER_PORT = 54321


def create_backend() -> "PyNMBackend":
from .app_pynm import PyNMState
Expand All @@ -26,7 +29,12 @@ def create_backend() -> "PyNMBackend":
return PyNMBackend(pynm_state=PyNMState())


def run_vite(shutdown_event: "Event", debug: bool = False) -> None:
def run_vite(
shutdown_event: "Event",
debug: bool = False,
scan: bool = False,
dev_port: int = DEV_SERVER_PORT,
) -> None:
"""Run Vite in a separate shell"""
import subprocess

Expand All @@ -38,6 +46,8 @@ def run_vite(shutdown_event: "Event", debug: bool = False) -> None:
logging.DEBUG if debug else logging.INFO,
)

os.environ["VITE_REACT_SCAN"] = "true" if scan else "false"

def output_reader(shutdown_event: "Event", process: subprocess.Popen):
logger.debug("Initialized output stream")
color = ansi_color(color="magenta", bright=True, styles=["BOLD"])
Expand Down Expand Up @@ -70,7 +80,7 @@ def read_stream(stream, stream_name):
subprocess_flags = subprocess.CREATE_NEW_PROCESS_GROUP if os.name == "nt" else 0

process = subprocess.Popen(
"bun run dev",
"bun run dev --port " + str(dev_port),
cwd="gui_dev",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down Expand Up @@ -175,7 +185,13 @@ class AppManager:
LAUNCH_FLAG = "PYNM_RUNNING"

def __init__(
self, debug: bool = False, dev: bool = True, run_in_webview=False
self,
debug: bool = False,
dev: bool = True,
run_in_webview=False,
scan=False,
server_port=SERVER_PORT,
dev_port=DEV_SERVER_PORT,
) -> None:
"""_summary_
Expand All @@ -190,6 +206,10 @@ def __init__(
self.debug = debug
self.dev = dev
self.run_in_webview = run_in_webview
self.scan = scan
self.server_port = server_port
self.dev_port = dev_port

self._reset()
# Prevent launching multiple instances of the app due to multiprocessing
# This allows the absence of a main guard in the main script
Expand Down Expand Up @@ -269,7 +289,12 @@ def launch(self) -> None:
self.logger.info("Starting Vite server...")
self.tasks["vite"] = mp.Process(
target=run_vite,
kwargs={"shutdown_event": self.shutdown_event, "debug": self.debug},
kwargs={
"shutdown_event": self.shutdown_event,
"debug": self.debug,
"scan": self.scan,
"dev_port": self.dev_port,
},
name="Vite",
)

Expand Down
1 change: 0 additions & 1 deletion py_neuromodulation/stream/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pydantic.functional_validators import ModelWrapValidatorHandler

from py_neuromodulation import logger, user_features
from types import SimpleNamespace

from py_neuromodulation.utils.types import (
BoolSelector,
Expand Down
3 changes: 1 addition & 2 deletions py_neuromodulation/utils/pydantic_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Sequence,
)
from typing_extensions import Unpack, TypedDict
from pydantic import BaseModel, ConfigDict, model_validator, model_serializer
from pydantic import BaseModel, model_validator, model_serializer

from pydantic_core import (
ErrorDetails,
Expand Down Expand Up @@ -414,7 +414,6 @@ def rewrite_error_locations(self, handler):
loc = loc[:root_idx] + loc[root_idx + 1 :]
err["loc"] = tuple(loc)
errors.append(err)
print(errors)
raise ValidationError.from_exception_data(
title="ValidationError", line_errors=errors
)
2 changes: 1 addition & 1 deletion py_neuromodulation/utils/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from os import PathLike
from math import isnan
from typing import Literal, TYPE_CHECKING, Any, TypeVar
from typing import Literal, TYPE_CHECKING, Any
from pydantic import BaseModel, ConfigDict, model_validator
from .pydantic_extensions import NMBaseModel, NMSequenceModel, NMField
from abc import abstractmethod
Expand Down

0 comments on commit f8375c5

Please sign in to comment.