-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DRAFT - DO NOT MERGE] Use ml tool output with Python Editor POC #1180
Draft
microbit-robert
wants to merge
3
commits into
main
Choose a base branch
from
ml-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,223 @@ | ||
/** | ||
* (c) 2021-2022, Micro:bit Educational Foundation and contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
import { | ||
Box, | ||
Collapse, | ||
Divider, | ||
Flex, | ||
HStack, | ||
List, | ||
ListItem, | ||
Select, | ||
Stack, | ||
Text, | ||
useDisclosure, | ||
} from "@chakra-ui/react"; | ||
import AreaHeading from "../common/AreaHeading"; | ||
import HeadedScrollablePanel from "../common/HeadedScrollablePanel"; | ||
import { docStyles } from "../common/documentation-styles"; | ||
import { useRouterTabSlug } from "../router-hooks"; | ||
import DocumentationHeading from "./common/DocumentationHeading"; | ||
import ShowMoreButton from "./common/ShowMoreButton"; | ||
import Highlight from "./reference/Highlight"; | ||
import CodeEmbed from "./common/CodeEmbed"; | ||
import OpenButton from "../project/OpenButton"; | ||
import { useModelData } from "./ml/use-model-data"; | ||
import { ChangeEvent, useCallback, useState } from "react"; | ||
|
||
interface ModelTopicEntry { | ||
name: string; | ||
text: string; | ||
code: string | ((actionName: string) => string); | ||
slug: string; | ||
detail?: string; | ||
alternativesLabel?: string; | ||
} | ||
|
||
const modelTopicEntries: ModelTopicEntry[] = [ | ||
{ | ||
name: "Class names", | ||
text: "Return the class names of the current model.", | ||
code: "import model\n\n\nnamesList = model.get_class_names()", | ||
slug: "class-names", | ||
}, | ||
{ | ||
name: "Current action", | ||
text: "Return the current recognised action.", | ||
code: "import model\n\n\ndisplay.scroll(model.current_action())", | ||
slug: "current-action", | ||
}, | ||
{ | ||
name: "Is action", | ||
text: "Check if an action is currently being performed.", | ||
code: (actionName) => | ||
`import model\n\n\nwhile True:\n if model.is_action('${actionName}'):\n display.scroll('Yes')`, | ||
slug: "is-action", | ||
detail: "Triggered on the current recognised action.", | ||
alternativesLabel: "Select action:", | ||
}, | ||
{ | ||
name: "Was action", | ||
text: "Check if an action was performed.", | ||
code: (actionName) => | ||
`import model\n\n\nwhile True:\n if model.was_action('${actionName}'):\n display.scroll('Yes')`, | ||
slug: "was-action", | ||
detail: "Triggered if the specified action was recognised since last time.", | ||
alternativesLabel: "Select action:", | ||
}, | ||
]; | ||
|
||
const ModelArea = () => { | ||
const [modelData] = useModelData(); | ||
return ( | ||
<HeadedScrollablePanel | ||
heading={ | ||
<AreaHeading | ||
name="Machine learning" | ||
description="Use the machine learning model that was trained" | ||
/> | ||
} | ||
> | ||
<Box | ||
p={5} | ||
pb={1} | ||
fontSize="sm" | ||
sx={{ | ||
...docStyles, | ||
}} | ||
> | ||
<Stack spacing={3}> | ||
<Text>Open your data.json file to get started.</Text> | ||
<OpenButton alignSelf="flex-start" fileType="mljson" mode="button" /> | ||
</Stack> | ||
</Box> | ||
{modelData.length > 0 && ( | ||
<> | ||
<Box | ||
p={5} | ||
pb={1} | ||
fontSize="sm" | ||
sx={{ | ||
...docStyles, | ||
}} | ||
> | ||
<Text>Some optional introduction text</Text> | ||
</Box> | ||
<List flex="1 1 auto"> | ||
{modelTopicEntries.map((entry) => ( | ||
<ListItem key={entry.slug}> | ||
<ModelTopicEntry content={entry} /> | ||
<Divider /> | ||
</ListItem> | ||
))} | ||
</List> | ||
</> | ||
)} | ||
</HeadedScrollablePanel> | ||
); | ||
}; | ||
|
||
interface MlItemProps { | ||
content: ModelTopicEntry; | ||
} | ||
|
||
const ModelTopicEntry = ({ content }: MlItemProps) => { | ||
const { name, text, slug, code, detail, alternativesLabel } = content; | ||
const hasMore = true; | ||
const disclosure = useDisclosure(); | ||
const [anchor] = useRouterTabSlug("model"); | ||
const [modelData] = useModelData(); | ||
const [selectedAlternative, setSelectedAlternative] = useState< | ||
string | undefined | ||
>(alternativesLabel ? modelData[0].ID.toString() : undefined); | ||
const [processedCode, setProcessedCode] = useState<string>(() => { | ||
return typeof code === "string" ? code : code(modelData[0].name); | ||
}); | ||
|
||
const handleSelectChange = useCallback( | ||
(e: ChangeEvent<HTMLSelectElement>) => { | ||
const actionName = modelData.find( | ||
(action) => action.ID === parseInt(e.currentTarget.value) | ||
)?.name; | ||
if (actionName && typeof code === "function") { | ||
setProcessedCode(code(actionName)); | ||
} | ||
setSelectedAlternative(e.currentTarget.value); | ||
}, | ||
[code, modelData] | ||
); | ||
return ( | ||
<Highlight | ||
anchor={anchor} | ||
id={name} | ||
active={anchor?.id === slug} | ||
disclosure={disclosure} | ||
> | ||
<Box | ||
fontSize="sm" | ||
p={5} | ||
pr={3} | ||
mt={1} | ||
mb={1} | ||
className="docs-code" | ||
sx={{ | ||
...docStyles, | ||
}} | ||
> | ||
<HStack justifyContent="space-between" flexWrap="nowrap"> | ||
<DocumentationHeading name={name} isV2Only={false} /> | ||
{hasMore && ( | ||
<ShowMoreButton | ||
isBrief | ||
onClick={disclosure.onToggle} | ||
isOpen={disclosure.isOpen} | ||
/> | ||
)} | ||
</HStack> | ||
<Stack spacing={3} mt={3}> | ||
<Text noOfLines={disclosure.isOpen ? undefined : 1}>{text}</Text> | ||
{alternativesLabel && ( | ||
<Flex wrap="wrap" as="label"> | ||
<Text alignSelf="center" mr={2} as="span"> | ||
{alternativesLabel} | ||
</Text> | ||
<Select | ||
w="fit-content" | ||
onChange={handleSelectChange} | ||
value={selectedAlternative} | ||
size="sm" | ||
> | ||
{modelData.map((action) => ( | ||
<option key={action.ID} value={action.ID}> | ||
{action.name} | ||
</option> | ||
))} | ||
</Select> | ||
</Flex> | ||
)} | ||
<CodeEmbed | ||
code={processedCode} | ||
parentSlug={slug} | ||
toolkitType="model" | ||
/> | ||
</Stack> | ||
{hasMore && ( | ||
<Collapse in={disclosure.isOpen}> | ||
<Stack | ||
spacing={3} | ||
pt={detail ? 3 : 0} | ||
noOfLines={!disclosure.isOpen ? 1 : undefined} | ||
> | ||
<Text>{detail}</Text> | ||
</Stack> | ||
</Collapse> | ||
)} | ||
</Box> | ||
</Highlight> | ||
); | ||
}; | ||
|
||
export default ModelArea; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole component is hacky. If we had a markdown alternative for rendering extension documentation, we'd use it here. It's pretty horrible to write the model topic entries in the Sanity format.