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

View File

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

View File

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