support jsImports
This commit is contained in:
parent
8842f7b25e
commit
64ed32a17c
|
@ -8,14 +8,7 @@ import {
|
||||||
} 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";
|
||||||
type jsImports =
|
|
||||||
| number
|
|
||||||
| string
|
|
||||||
| jsImports[]
|
|
||||||
| {
|
|
||||||
[k: string]: jsImports;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface SquiggleChartProps {
|
export interface SquiggleChartProps {
|
||||||
/** The input string for squiggle */
|
/** The input string for squiggle */
|
||||||
|
@ -38,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 */
|
||||||
|
@ -61,7 +54,7 @@ export interface SquiggleChartProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultOnChange = () => {};
|
const defaultOnChange = () => {};
|
||||||
const defaultImports = {};
|
const defaultImports: JsImports = {};
|
||||||
|
|
||||||
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
|
||||||
({
|
({
|
||||||
|
|
|
@ -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,18 +1,11 @@
|
||||||
import { environment, SqProject, 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";
|
||||||
export type jsImports =
|
|
||||||
| number
|
|
||||||
| string
|
|
||||||
| jsImports[]
|
|
||||||
| {
|
|
||||||
[k: string]: 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;
|
||||||
};
|
};
|
||||||
|
@ -25,12 +18,10 @@ export const useSquiggle = (args: SquiggleArgs) => {
|
||||||
if (args.environment) {
|
if (args.environment) {
|
||||||
project.setEnvironment(args.environment);
|
project.setEnvironment(args.environment);
|
||||||
}
|
}
|
||||||
if (args.jsImports) {
|
if (args.jsImports && Object.keys(args.jsImports).length) {
|
||||||
console.log(JSON.stringify(args.jsImports));
|
const importsSource = jsImportsToSquiggleCode(args.jsImports);
|
||||||
project.setSource(
|
project.setSource("imports", importsSource);
|
||||||
"zzz", // due to bug in topology implementation, can be renamed later
|
project.setContinues("main", ["imports"]);
|
||||||
"imports = " + JSON.stringify(args.jsImports)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
project.run("main");
|
project.run("main");
|
||||||
const result = project.getResult("main");
|
const result = project.getResult("main");
|
||||||
|
@ -38,12 +29,7 @@ export const useSquiggle = (args: SquiggleArgs) => {
|
||||||
return { result, bindings };
|
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;
|
||||||
|
|
41
packages/components/src/lib/jsImports.ts
Normal file
41
packages/components/src/lib/jsImports.ts
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
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]) => `${k}:${jsImportsValueToSquiggleCode(v)},`)
|
||||||
|
.join("") +
|
||||||
|
"}"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return "0"; // squiggle doesn't support empty `{}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const jsImportsToSquiggleCode = (v: JsImports) => {
|
||||||
|
return Object.entries(v)
|
||||||
|
.map(([k, v]) => `$${k} = ${jsImportsValueToSquiggleCode(v)}\n`)
|
||||||
|
.join("");
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user