Merge pull request #1052 from quantified-uncertainty/reducer-project-imports
Reducer project imports
This commit is contained in:
commit
130af680f6
|
@ -4,11 +4,11 @@ import {
|
|||
environment,
|
||||
defaultEnvironment,
|
||||
resultMap,
|
||||
SqValueLocation,
|
||||
SqValueTag,
|
||||
} from "@quri/squiggle-lang";
|
||||
import { useSquiggle } from "../lib/hooks";
|
||||
import { SquiggleViewer } from "./SquiggleViewer";
|
||||
import { JsImports } from "../lib/jsImports";
|
||||
|
||||
export interface SquiggleChartProps {
|
||||
/** The input string for squiggle */
|
||||
|
@ -31,7 +31,7 @@ export interface SquiggleChartProps {
|
|||
width?: number;
|
||||
height?: number;
|
||||
/** JS imported parameters */
|
||||
// jsImports?: jsImports;
|
||||
jsImports?: JsImports;
|
||||
/** Whether to show a summary of the distribution */
|
||||
showSummary?: boolean;
|
||||
/** Set the x scale to be logarithmic by deault */
|
||||
|
@ -54,6 +54,7 @@ export interface SquiggleChartProps {
|
|||
}
|
||||
|
||||
const defaultOnChange = () => {};
|
||||
const defaultImports: JsImports = {};
|
||||
|
||||
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
||||
({
|
||||
|
@ -62,7 +63,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
|||
environment,
|
||||
onChange = defaultOnChange, // defaultOnChange must be constant, don't move its definition here
|
||||
height = 200,
|
||||
// jsImports = defaultImports,
|
||||
jsImports = defaultImports,
|
||||
showSummary = false,
|
||||
width,
|
||||
logX = false,
|
||||
|
@ -81,7 +82,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
|||
const { result, bindings } = useSquiggle({
|
||||
code,
|
||||
environment,
|
||||
// jsImports,
|
||||
jsImports,
|
||||
onChange,
|
||||
executionId,
|
||||
});
|
||||
|
|
|
@ -39,6 +39,7 @@ import { ViewSettings, viewSettingsSchema } from "./ViewSettings";
|
|||
import { HeadedSection } from "./ui/HeadedSection";
|
||||
import { defaultTickFormat } from "../lib/distributionSpecBuilder";
|
||||
import { Button } from "./ui/Button";
|
||||
import { JsImports } from "../lib/jsImports";
|
||||
|
||||
type PlaygroundProps = SquiggleChartProps & {
|
||||
/** The initial squiggle string to put in the playground */
|
||||
|
@ -112,8 +113,8 @@ const SamplingSettings: React.FC<{ register: UseFormRegister<FormFields> }> = ({
|
|||
);
|
||||
|
||||
const InputVariablesSettings: React.FC<{
|
||||
initialImports: any; // TODO - any json type
|
||||
setImports: (imports: any) => void;
|
||||
initialImports: JsImports;
|
||||
setImports: (imports: JsImports) => void;
|
||||
}> = ({ initialImports, setImports }) => {
|
||||
const [importString, setImportString] = useState(() =>
|
||||
JSON.stringify(initialImports)
|
||||
|
@ -122,7 +123,7 @@ const InputVariablesSettings: React.FC<{
|
|||
|
||||
const onChange = (value: string) => {
|
||||
setImportString(value);
|
||||
let imports = {} as any;
|
||||
let imports = {};
|
||||
try {
|
||||
imports = JSON.parse(value);
|
||||
setImportsAreValid(true);
|
||||
|
@ -251,7 +252,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
|
|||
onChange: onCodeChange,
|
||||
});
|
||||
|
||||
const [imports, setImports] = useState({});
|
||||
const [imports, setImports] = useState<JsImports>({});
|
||||
|
||||
const { register, control } = useForm({
|
||||
resolver: yupResolver(schema),
|
||||
|
@ -309,7 +310,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
|
|||
executionId={executionId}
|
||||
environment={env}
|
||||
{...vars}
|
||||
// jsImports={imports}
|
||||
jsImports={imports}
|
||||
enableLocalSettings={true}
|
||||
/>
|
||||
</div>
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { environment, run, SqValue } from "@quri/squiggle-lang";
|
||||
import { environment, SqProject, SqValue } from "@quri/squiggle-lang";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { JsImports, jsImportsToSquiggleCode } from "../jsImports";
|
||||
|
||||
type SquiggleArgs = {
|
||||
code: string;
|
||||
executionId?: number;
|
||||
// jsImports?: jsImports;
|
||||
jsImports?: JsImports;
|
||||
environment?: environment;
|
||||
onChange?: (expr: SqValue | undefined) => void;
|
||||
};
|
||||
|
@ -12,18 +13,23 @@ type SquiggleArgs = {
|
|||
export const useSquiggle = (args: SquiggleArgs) => {
|
||||
const result = useMemo(
|
||||
() => {
|
||||
const result = run(args.code, {
|
||||
environment: args.environment,
|
||||
});
|
||||
return result;
|
||||
const project = SqProject.create();
|
||||
project.setSource("main", args.code);
|
||||
if (args.environment) {
|
||||
project.setEnvironment(args.environment);
|
||||
}
|
||||
if (args.jsImports && Object.keys(args.jsImports).length) {
|
||||
const importsSource = jsImportsToSquiggleCode(args.jsImports);
|
||||
project.setSource("imports", importsSource);
|
||||
project.setContinues("main", ["imports"]);
|
||||
}
|
||||
project.run("main");
|
||||
const result = project.getResult("main");
|
||||
const bindings = project.getBindings("main");
|
||||
return { result, bindings };
|
||||
},
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[
|
||||
args.code,
|
||||
args.environment,
|
||||
// args.jsImports,
|
||||
args.executionId,
|
||||
]
|
||||
[args.code, args.environment, args.jsImports, args.executionId]
|
||||
);
|
||||
|
||||
const { onChange } = args;
|
||||
|
|
51
packages/components/src/lib/jsImports.ts
Normal file
51
packages/components/src/lib/jsImports.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
type JsImportsValue =
|
||||
| number
|
||||
| string
|
||||
| JsImportsValue[]
|
||||
| {
|
||||
[k: string]: JsImportsValue;
|
||||
};
|
||||
|
||||
export type JsImports = {
|
||||
[k: string]: JsImportsValue;
|
||||
};
|
||||
|
||||
const quote = (arg: string) => `"${arg.replace(new RegExp('"', "g"), '\\"')}"`;
|
||||
|
||||
const jsImportsValueToSquiggleCode = (v: JsImportsValue): string => {
|
||||
if (typeof v === "number") {
|
||||
return String(v);
|
||||
} else if (typeof v === "string") {
|
||||
return quote(v);
|
||||
} else if (v instanceof Array) {
|
||||
return "[" + v.map((x) => jsImportsValueToSquiggleCode(x)) + "]";
|
||||
} else {
|
||||
if (Object.keys(v).length) {
|
||||
return (
|
||||
"{" +
|
||||
Object.entries(v)
|
||||
.map(([k, v]) => `${quote(k)}:${jsImportsValueToSquiggleCode(v)},`)
|
||||
.join("") +
|
||||
"}"
|
||||
);
|
||||
} else {
|
||||
return "0"; // squiggle doesn't support empty `{}`
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const jsImportsToSquiggleCode = (v: JsImports) => {
|
||||
const validId = new RegExp("[a-zA-Z][[a-zA-Z0-9]*");
|
||||
let result = Object.entries(v)
|
||||
.map(([k, v]) => {
|
||||
if (!k.match(validId)) {
|
||||
return ""; // skipping without warnings; can be improved
|
||||
}
|
||||
return `$${k} = ${jsImportsValueToSquiggleCode(v)}\n`;
|
||||
})
|
||||
.join("");
|
||||
if (!result) {
|
||||
result = "$__no_valid_imports__ = 1"; // without this generated squiggle code can be invalid
|
||||
}
|
||||
return result;
|
||||
};
|
|
@ -62,14 +62,6 @@ export class SqProject {
|
|||
return RSProject.setContinues(this._value, sourceId, continues);
|
||||
}
|
||||
|
||||
getDependencies(sourceId: string) {
|
||||
return RSProject.getDependencies(this._value, sourceId);
|
||||
}
|
||||
|
||||
getDependents(sourceId: string) {
|
||||
return RSProject.getDependents(this._value, sourceId);
|
||||
}
|
||||
|
||||
getRunOrder() {
|
||||
return RSProject.getRunOrder(this._value);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user