Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useActionData,
useFetcher,
useLoaderData,
useNavigate,
useSearchParams,
useSubmit,
} from "@remix-run/react";
Expand Down Expand Up @@ -193,11 +194,27 @@ function App() {
const [searchParams] = useSearchParams();
const loaderData = useLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const submit = useSubmit();
const { base64Body, children, state, channels } = actionData ?? loaderData;
const formRef = useRef<HTMLFormElement>(null);
const realtimeEvent = useRealtimeChannels({ channels });
const fetcher = useFetcher();
const submit = useSubmit();
const navigate = useNavigate();

if (typeof window !== "undefined") {
// @ts-ignore
window.gui = {
session_state: state,
navigate,
fetcher,
submit() {
if (formRef.current) submit(formRef.current, ...arguments);
},
rerun() {
if (formRef.current) submit(formRef.current);
},
};
}

useEffect(() => {
if (!base64Body) return;
Expand Down
9 changes: 4 additions & 5 deletions app/gooeyInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useRef, useState } from "react";
import { RenderedMarkdown } from "~/renderedMarkdown";
import { TooltipPlacement } from "./components/GooeyTooltip";
import { useSubmit } from "@remix-run/react";

export function InputLabel({
label,
Expand Down Expand Up @@ -242,17 +243,15 @@ export function useGooeyCheckedInput({
}

export function loadEventHandlers<T>(
value: T,
setValue: (value: T) => void,
value?: T,
setValue?: (value: T) => void,
args?: Record<string, any>
) {
if (!args) return;
for (const [attr, body] of Object.entries(args)) {
if (!attr.startsWith("on")) continue;
if (!attr.startsWith("on") || typeof body !== "string") continue;
args[attr] = (event: React.SyntheticEvent) => {
// new Function(...args, body)
const fn = new Function("event", "value", "setValue", body);
// fn.call(thisArg, ...args)
return fn.call(event.currentTarget, event.nativeEvent, value, setValue);
};
}
Expand Down
22 changes: 12 additions & 10 deletions app/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GooeyRadio,
GooeyTextarea,
InputLabel,
loadEventHandlers,
} from "~/gooeyInput";
import { useJsonFormInput } from "~/jsonFormInput";
import { RenderedHTML } from "~/renderedHTML";
Expand Down Expand Up @@ -133,6 +134,7 @@ function RenderedTreeNode({
state: Record<string, any>;
}) {
const { name, props, children } = node;
loadEventHandlers(undefined, undefined, props);

switch (name) {
case "":
Expand Down Expand Up @@ -469,7 +471,7 @@ function RenderedTreeNode({
}
case "script": {
const { src, args } = props;
return <ExecJs src={src} args={args}></ExecJs>;
return <ExecJs state={state} src={src} args={args}></ExecJs>;
}
case "plotly-chart": {
const { chart } = props;
Expand Down Expand Up @@ -512,20 +514,20 @@ function RenderedTreeNode({
}
}

function ExecJs({ src, args }: { args: any; src: any }) {
const submit = useSubmit();

args.gooeyRefresh = () => {
const elem = document.getElementById("gooey-form");
if (elem) submit(elem as HTMLFormElement, ...arguments);
};

function ExecJs({
src,
args,
state,
}: {
src: string;
args: Record<string, any>;
state: Record<string, any>;
}) {
useEffect(() => {
// eslint-disable-next-line no-new-func
const fn = new Function(...Object.keys(args), src);
fn(...Object.values(args));
}, [src, args]);

return null;
}

Expand Down
2 changes: 0 additions & 2 deletions py/gooey_gui/components/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,6 @@ def button(
value="yes",
name=key,
label=dedent(label),
help=help,
tooltipPlacement=tooltip_placement,
disabled=disabled,
className=className,
**props,
Expand Down