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,
|
environment,
|
||||||
defaultEnvironment,
|
defaultEnvironment,
|
||||||
resultMap,
|
resultMap,
|
||||||
SqValueLocation,
|
|
||||||
SqValueTag,
|
SqValueTag,
|
||||||
} from "@quri/squiggle-lang";
|
} from "@quri/squiggle-lang";
|
||||||
import { useSquiggle } from "../lib/hooks";
|
import { useSquiggle } from "../lib/hooks";
|
||||||
import { SquiggleViewer } from "./SquiggleViewer";
|
import { SquiggleViewer } from "./SquiggleViewer";
|
||||||
|
import { JsImports } from "../lib/jsImports";
|
||||||
|
|
||||||
export interface SquiggleChartProps {
|
export interface SquiggleChartProps {
|
||||||
/** The input string for squiggle */
|
/** The input string for squiggle */
|
||||||
|
@ -31,7 +31,7 @@ export interface SquiggleChartProps {
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
/** JS imported parameters */
|
/** JS imported parameters */
|
||||||
// jsImports?: jsImports;
|
jsImports?: JsImports;
|
||||||
/** Whether to show a summary of the distribution */
|
/** Whether to show a summary of the distribution */
|
||||||
showSummary?: boolean;
|
showSummary?: boolean;
|
||||||
/** Set the x scale to be logarithmic by deault */
|
/** Set the x scale to be logarithmic by deault */
|
||||||
|
@ -54,6 +54,7 @@ export interface SquiggleChartProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultOnChange = () => {};
|
const defaultOnChange = () => {};
|
||||||
|
const defaultImports: JsImports = {};
|
||||||
|
|
||||||
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
||||||
({
|
({
|
||||||
|
@ -62,7 +63,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
||||||
environment,
|
environment,
|
||||||
onChange = defaultOnChange, // defaultOnChange must be constant, don't move its definition here
|
onChange = defaultOnChange, // defaultOnChange must be constant, don't move its definition here
|
||||||
height = 200,
|
height = 200,
|
||||||
// jsImports = defaultImports,
|
jsImports = defaultImports,
|
||||||
showSummary = false,
|
showSummary = false,
|
||||||
width,
|
width,
|
||||||
logX = false,
|
logX = false,
|
||||||
|
@ -81,7 +82,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
||||||
const { result, bindings } = useSquiggle({
|
const { result, bindings } = useSquiggle({
|
||||||
code,
|
code,
|
||||||
environment,
|
environment,
|
||||||
// jsImports,
|
jsImports,
|
||||||
onChange,
|
onChange,
|
||||||
executionId,
|
executionId,
|
||||||
});
|
});
|
||||||
|
|
|
@ -39,6 +39,7 @@ import { ViewSettings, viewSettingsSchema } from "./ViewSettings";
|
||||||
import { HeadedSection } from "./ui/HeadedSection";
|
import { HeadedSection } from "./ui/HeadedSection";
|
||||||
import { defaultTickFormat } from "../lib/distributionSpecBuilder";
|
import { defaultTickFormat } from "../lib/distributionSpecBuilder";
|
||||||
import { Button } from "./ui/Button";
|
import { Button } from "./ui/Button";
|
||||||
|
import { JsImports } from "../lib/jsImports";
|
||||||
|
|
||||||
type PlaygroundProps = SquiggleChartProps & {
|
type PlaygroundProps = SquiggleChartProps & {
|
||||||
/** The initial squiggle string to put in the playground */
|
/** The initial squiggle string to put in the playground */
|
||||||
|
@ -112,8 +113,8 @@ const SamplingSettings: React.FC<{ register: UseFormRegister<FormFields> }> = ({
|
||||||
);
|
);
|
||||||
|
|
||||||
const InputVariablesSettings: React.FC<{
|
const InputVariablesSettings: React.FC<{
|
||||||
initialImports: any; // TODO - any json type
|
initialImports: JsImports;
|
||||||
setImports: (imports: any) => void;
|
setImports: (imports: JsImports) => void;
|
||||||
}> = ({ initialImports, setImports }) => {
|
}> = ({ initialImports, setImports }) => {
|
||||||
const [importString, setImportString] = useState(() =>
|
const [importString, setImportString] = useState(() =>
|
||||||
JSON.stringify(initialImports)
|
JSON.stringify(initialImports)
|
||||||
|
@ -122,7 +123,7 @@ const InputVariablesSettings: React.FC<{
|
||||||
|
|
||||||
const onChange = (value: string) => {
|
const onChange = (value: string) => {
|
||||||
setImportString(value);
|
setImportString(value);
|
||||||
let imports = {} as any;
|
let imports = {};
|
||||||
try {
|
try {
|
||||||
imports = JSON.parse(value);
|
imports = JSON.parse(value);
|
||||||
setImportsAreValid(true);
|
setImportsAreValid(true);
|
||||||
|
@ -251,7 +252,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
|
||||||
onChange: onCodeChange,
|
onChange: onCodeChange,
|
||||||
});
|
});
|
||||||
|
|
||||||
const [imports, setImports] = useState({});
|
const [imports, setImports] = useState<JsImports>({});
|
||||||
|
|
||||||
const { register, control } = useForm({
|
const { register, control } = useForm({
|
||||||
resolver: yupResolver(schema),
|
resolver: yupResolver(schema),
|
||||||
|
@ -309,7 +310,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
|
||||||
executionId={executionId}
|
executionId={executionId}
|
||||||
environment={env}
|
environment={env}
|
||||||
{...vars}
|
{...vars}
|
||||||
// jsImports={imports}
|
jsImports={imports}
|
||||||
enableLocalSettings={true}
|
enableLocalSettings={true}
|
||||||
/>
|
/>
|
||||||
</div>
|
</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 { useEffect, useMemo } from "react";
|
||||||
|
import { JsImports, jsImportsToSquiggleCode } from "../jsImports";
|
||||||
|
|
||||||
type SquiggleArgs = {
|
type SquiggleArgs = {
|
||||||
code: string;
|
code: string;
|
||||||
executionId?: number;
|
executionId?: number;
|
||||||
// jsImports?: jsImports;
|
jsImports?: JsImports;
|
||||||
environment?: environment;
|
environment?: environment;
|
||||||
onChange?: (expr: SqValue | undefined) => void;
|
onChange?: (expr: SqValue | undefined) => void;
|
||||||
};
|
};
|
||||||
|
@ -12,18 +13,23 @@ type SquiggleArgs = {
|
||||||
export const useSquiggle = (args: SquiggleArgs) => {
|
export const useSquiggle = (args: SquiggleArgs) => {
|
||||||
const result = useMemo(
|
const result = useMemo(
|
||||||
() => {
|
() => {
|
||||||
const result = run(args.code, {
|
const project = SqProject.create();
|
||||||
environment: args.environment,
|
project.setSource("main", args.code);
|
||||||
});
|
if (args.environment) {
|
||||||
return result;
|
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
|
// 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;
|
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);
|
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() {
|
getRunOrder() {
|
||||||
return RSProject.getRunOrder(this._value);
|
return RSProject.getRunOrder(this._value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user