Add parameters to components interface

This commit is contained in:
Sam Nolan 2022-04-29 19:02:43 +00:00
parent d4f929367d
commit 39be07cac0
3 changed files with 47 additions and 14 deletions

View File

@ -3,16 +3,17 @@ import _ from "lodash";
import styled from "styled-components";
import {
run,
runPartial,
errorValueToString,
squiggleExpression,
bindings,
samplingParams,
parameters,
defaultParameters,
defaultBindings,
} from "@quri/squiggle-lang";
import { NumberShower } from "./NumberShower";
import { DistributionChart } from "./DistributionChart";
import { ErrorBox } from "./ErrorBox";
import useSize from "@react-hook/size";
const variableBox = {
Component: styled.div`
@ -152,6 +153,8 @@ export interface SquiggleChartProps {
height?: number;
/** Bindings of previous variables declared */
bindings?: bindings;
/** JS imported parameters */
parameters?: parameters;
}
const ChartWrapper = styled.div`
@ -166,14 +169,20 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
outputXYPoints = 1000,
onChange = () => {},
height = 60,
bindings = {},
bindings = defaultBindings,
parameters = defaultParameters,
width = NaN,
}: SquiggleChartProps) => {
let samplingInputs: samplingParams = {
sampleCount: sampleCount,
xyPointLength: outputXYPoints,
};
let expressionResult = run(squiggleString, bindings, samplingInputs);
let expressionResult = run(
squiggleString,
bindings,
samplingInputs,
parameters
);
let internal: JSX.Element;
if (expressionResult.tag === "Ok") {
let expression = expressionResult.value;

View File

@ -3,8 +3,17 @@ import * as ReactDOM from "react-dom";
import { SquiggleChart } from "./SquiggleChart";
import { CodeEditor } from "./CodeEditor";
import styled from "styled-components";
import type { squiggleExpression, bindings } from "@quri/squiggle-lang";
import { runPartial, errorValueToString } from "@quri/squiggle-lang";
import type {
squiggleExpression,
bindings,
parameters,
} from "@quri/squiggle-lang";
import {
runPartial,
errorValueToString,
defaultParameters,
defaultBindings,
} from "@quri/squiggle-lang";
import { ErrorBox } from "./ErrorBox";
export interface SquiggleEditorProps {
@ -30,6 +39,8 @@ export interface SquiggleEditorProps {
width: number;
/** Previous variable declarations */
bindings: bindings;
/** JS Imported parameters */
parameters: parameters;
}
const Input = styled.div`
@ -50,7 +61,8 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
diagramCount,
onChange,
environment,
bindings = {},
bindings = defaultBindings,
parameters = defaultParameters,
}: SquiggleEditorProps) => {
let [expression, setExpression] = React.useState(initialSquiggleString);
return (
@ -77,6 +89,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
environment={environment}
onChange={onChange}
bindings={bindings}
parameters={parameters}
/>
</div>
);
@ -134,13 +147,16 @@ export interface SquigglePartialProps {
/** The width of the element */
width: number;
/** Previously declared variables */
bindings: bindings;
bindings?: bindings;
/** Parameters imported from js */
parameters?: parameters;
}
export let SquigglePartial: React.FC<SquigglePartialProps> = ({
initialSquiggleString = "",
onChange,
bindings,
bindings = defaultBindings,
parameters = defaultParameters,
}: SquigglePartialProps) => {
let [expression, setExpression] = React.useState(initialSquiggleString);
let squiggleResult = runPartial(expression, bindings);

View File

@ -48,7 +48,12 @@ import {
Constructors_pointwiseLogarithm,
Constructors_pointwisePower,
} from "../rescript/Distributions/DistributionOperation/DistributionOperation.gen";
export type { samplingParams, errorValue, externalBindings as bindings };
export type {
samplingParams,
errorValue,
externalBindings as bindings,
parameters,
};
export let defaultSamplingInputs: samplingParams = {
sampleCount: 10000,
@ -102,8 +107,8 @@ export function run(
samplingInputs?: samplingParams,
parameters?: parameters
): result<squiggleExpression, errorValue> {
let b = bindings ? bindings : {};
let p = parameters ? parameters : {};
let b = bindings ? bindings : defaultBindings;
let p = parameters ? parameters : defaultParameters;
let si: samplingParams = samplingInputs
? samplingInputs
: defaultSamplingInputs;
@ -120,8 +125,8 @@ export function runPartial(
_samplingInputs?: samplingParams,
parameters?: parameters
): result<externalBindings, errorValue> {
let b = bindings ? bindings : {};
let p = parameters ? parameters : {};
let b = bindings ? bindings : defaultBindings;
let p = parameters ? parameters : defaultParameters;
return evaluatePartialUsingExternalBindings(
squiggleString,
@ -144,6 +149,9 @@ function mergeParameters(
type parameters = { [key: string]: jsValue };
export let defaultParameters: parameters = {};
export let defaultBindings: externalBindings = {};
type jsValue =
| string
| number