-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1258 from facebookresearch/remote-procedure-examp…
…les-fixes Updated remote procedure examples; Refactored remote procedure examples and blueprint
- Loading branch information
Showing
57 changed files
with
852 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
examples/remote_procedure/elementary_remote_procedure/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!--- | ||
Copyright (c) Meta Platforms and its affiliates. | ||
This source code is licensed under the MIT license found in the | ||
LICENSE file in the root directory of this source tree. | ||
--> | ||
|
||
# Mephisto Elementary remote procedure example | ||
|
||
This task is a _very simplistic_ bare-bones example of being able to set up a task where the frontend directly connects to the backend | ||
using the `useMephistoRemoteProcedureTask` hook from the `mephisto-task` package. | ||
It should serve as a decent example of how to get a `RemoteProcedureBlueprint` task up off the ground. | ||
|
||
Deploying the task as-is brings up a page where the user needs to click the "query backend" button enough times | ||
for the task to be considered ready to submit. | ||
|
||
It makes use of the function_registry to use a `"handle_with_model"` method that, admittedly, doesn't use any models, | ||
but hopefully demonstrates where a model can fit into this type of task. | ||
|
||
As it stands, these queries are still just a single request to single response model, | ||
but that should be sufficient for most tasks that want to have a backend in-the-loop for an otherwise frontend-heavy task. | ||
|
||
## End-to-end example | ||
|
||
Now let's see how the whole end-to-end list of commands looks like for the example `Elementary remote procedure`: | ||
|
||
```shell | ||
# 1. In your console | ||
|
||
docker-compose -f docker/docker-compose.dev.yml up | ||
docker exec -it mephisto_dc bash | ||
|
||
# 2.Inside Docker container | ||
|
||
cd /mephisto/examples/remote_procedure/elementary_remote_procedure | ||
python ./run_task__local__inhouse.py | ||
``` |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
10 changes: 5 additions & 5 deletions
10
...te_procedure/template/webapp/package.json → ...tary_remote_procedure/webapp/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...re/elementary_remote_procedure/webapp/src/components/core_components_remote_procedure.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) Meta Platforms and its affiliates. | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from "react"; | ||
|
||
function LoadingScreen() { | ||
return <Directions>Loading...</Directions>; | ||
} | ||
|
||
function Directions({ children }) { | ||
return ( | ||
<div className={"card mb-4"}> | ||
<div className={"card-body container"}>{children}</div> | ||
</div> | ||
); | ||
} | ||
|
||
function Instructions() { | ||
return ( | ||
<div className={"mb-5"}> | ||
<h1 data-cy={"directions-header"}> | ||
Elementary example of remote procedure | ||
</h1> | ||
|
||
<h5 data-cy={"subheader-paragraph"}> | ||
This is a simple task to demonstrate communication with the server | ||
</h5> | ||
|
||
<p data-cy={"directions-paragraph"}> | ||
To submit this task, you must make a few backend queries first | ||
</p> | ||
</div> | ||
); | ||
} | ||
|
||
function ElementaryRemoteProcedureTaskFrontend({ | ||
taskData, | ||
handleRemoteCall, | ||
handleSubmit, | ||
finalResults = null, | ||
}) { | ||
const inReviewState = finalResults !== null; | ||
|
||
if (!inReviewState && !taskData) { | ||
return <LoadingScreen />; | ||
} | ||
|
||
const [queryCount, setQueryCount] = React.useState(0); | ||
let canSubmit = queryCount > 3; | ||
|
||
const disabledQueryButton = inReviewState; | ||
const disabledSubmitButton = inReviewState || !canSubmit; | ||
|
||
return ( | ||
<div> | ||
{/* Final result of template example (needed only for TaskReview app) */} | ||
{inReviewState && ( | ||
<div className={"alert alert-success mb-5 col-6"}> | ||
Task result: {finalResults.backendActionsDone} | ||
</div> | ||
)} | ||
|
||
<Instructions /> | ||
|
||
<button | ||
className={"btn btn-primary me-3"} | ||
onClick={() => { | ||
setQueryCount(queryCount + 1); | ||
handleRemoteCall({ | ||
arg1: "hello", | ||
arg2: "goodbye", | ||
arg3: queryCount, | ||
}).then((response) => alert(JSON.stringify(response))); | ||
}} | ||
disabled={disabledQueryButton} | ||
data-cy={"query-backend-button"} | ||
> | ||
Query Backend | ||
</button> | ||
|
||
<button | ||
className={"btn btn-success"} | ||
onClick={() => | ||
handleSubmit({ | ||
backendActionsDone: queryCount, | ||
}) | ||
} | ||
disabled={disabledSubmitButton} | ||
data-cy={"submit-button"} | ||
> | ||
Submit Task | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export { LoadingScreen, ElementaryRemoteProcedureTaskFrontend }; |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
examples/remote_procedure/elementary_remote_procedure/webapp/src/main_remote_procedure.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) Meta Platforms and its affiliates. | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import "bootstrap/dist/css/bootstrap.min.css"; | ||
import "./app_remote_procedure.jsx"; | ||
import "./css/style.css"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
examples/remote_procedure/elementary_remote_procedure/webapp/src/reviewapp.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { ElementaryRemoteProcedureTaskFrontend } from "./components/core_components_remote_procedure.jsx"; | ||
|
||
function ReviewApp() { | ||
const appRef = React.useRef(null); | ||
const [reviewData, setReviewData] = React.useState(null); | ||
|
||
// Requirement #1. Render review components after receiving Task data via message | ||
window.onmessage = function (e) { | ||
const data = JSON.parse(e.data); | ||
setReviewData(data["REVIEW_DATA"]); | ||
}; | ||
|
||
// Requirement #2. Resize iframe height to fit its content | ||
React.useLayoutEffect(() => { | ||
function updateSize() { | ||
if (appRef.current) { | ||
window.top.postMessage( | ||
JSON.stringify({ | ||
IFRAME_DATA: { | ||
height: appRef.current.offsetHeight, | ||
}, | ||
}), | ||
"*" | ||
); | ||
} | ||
} | ||
window.addEventListener("resize", updateSize); | ||
updateSize(); | ||
// HACK: Catch-all resize, if normal resizes failed (e.g. slow acync loading of images) | ||
setTimeout(() => { | ||
updateSize(); | ||
}, 3000); | ||
return () => window.removeEventListener("resize", updateSize); | ||
}, []); | ||
|
||
// Requirement #3. This component must return a div with `ref={appRef}` | ||
// so we can get displayed height of this component (for iframe resizing) | ||
return ( | ||
<div ref={appRef}> | ||
{reviewData ? ( | ||
<div className={"container"} id={"ui-container"}> | ||
<ElementaryRemoteProcedureTaskFrontend | ||
finalResults={reviewData["outputs"]} | ||
/> | ||
</div> | ||
) : ( | ||
<div>Loading...</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
ReactDOM.render(<ReviewApp />, document.getElementById("app")); |
Oops, something went wrong.