-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add classification component #612
Draft
wch
wants to merge
13
commits into
main
Choose a base branch
from
label-component
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
49b5845
Update eslint rules
wch 59c9c04
Update JS build script
wch a78da65
Add output_classification_label
wch f6580fa
Add render.ml.classification_label
wch 5570cb9
Update gitignore
wch 384c35f
Remove unneeded print
wch 3d0401f
Remove old comments
wch 859863e
Update app layout
wch bda6fb6
Add display_winner parameter
wch 634fffc
Turn classification_label into static component
wch d1a953e
Add max_items parameter
wch 96772f7
Add docstring for classification_label
wch f65b113
Fix example code blocks
wch 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
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 |
---|---|---|
|
@@ -102,8 +102,7 @@ ENV/ | |
|
||
typings/ | ||
|
||
docs/source/generated/ | ||
docs/source/reference/ | ||
dist/ | ||
|
||
.DS_Store | ||
.Rproj.user |
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,55 @@ | ||
from shiny import App, Inputs, Outputs, Session, render, ui | ||
|
||
app_ui = ui.page_fluid( | ||
ui.layout_sidebar( | ||
ui.panel_sidebar( | ||
ui.input_slider("lion", "Lion value:", min=0, max=100, value=60, step=1), | ||
), | ||
ui.panel_main( | ||
ui.h3("Dynamic output, display_winner=True"), | ||
ui.output_ui("label1"), | ||
ui.h3( | ||
"Static output, display_winner=True, max_items=2", | ||
style="margin-top: 3rem;", | ||
), | ||
ui.ml.classification_label( | ||
{ | ||
"Tigers": 32, | ||
"Lions": 60, | ||
"Bears": 15, | ||
}, | ||
display_winner=True, | ||
max_items=2, | ||
), | ||
ui.h3("Static output, sort=False"), | ||
ui.ml.classification_label( | ||
{ | ||
"Tigers": 32, | ||
"Lions": 60, | ||
"Bears": 15, | ||
}, | ||
max_items=3, | ||
sort=False, | ||
), | ||
), | ||
) | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
@output | ||
@render.ui | ||
def label1(): | ||
return ( | ||
ui.ml.classification_label( | ||
value={ | ||
"Tigers": 32, | ||
"Lions": input.lion(), | ||
"Bears": 15, | ||
}, | ||
display_winner=True, | ||
), | ||
) | ||
|
||
|
||
app = App(app_ui, server) |
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 |
---|---|---|
@@ -1,27 +1,60 @@ | ||
import { BuildOptions, build } from "esbuild"; | ||
import esbuild from "esbuild"; | ||
import { sassPlugin } from "esbuild-sass-plugin"; | ||
import * as fs from "node:fs"; | ||
|
||
async function bundle() { | ||
try { | ||
const options: BuildOptions = { | ||
entryPoints: { dataframe: "dataframe/index.tsx" }, | ||
format: "esm", | ||
bundle: true, | ||
outdir: "../shiny/www/shared/dataframe", | ||
minify: true, | ||
sourcemap: true, | ||
plugins: [sassPlugin({ type: "css-text", sourceMap: false })], | ||
metafile: true, | ||
}; | ||
// Set a boolean value of watch to true if the flag --watch is provided when the file is run from the command line. | ||
// E.g. tsx scripts/build.ts --watch | ||
const watch: boolean = process.argv.includes("--watch"); | ||
const minify: boolean = process.argv.includes("--minify"); | ||
const metafile: boolean = process.argv.includes("--metafile"); | ||
|
||
const result = await build(options); | ||
console.log("Build completed successfully!"); | ||
// console.log("Output:", result); | ||
fs.writeFileSync("esbuild-metadata.json", JSON.stringify(result.metafile)); | ||
} catch (error) { | ||
console.error("Build failed:", error); | ||
} | ||
} | ||
const rebuildLoggerPlugin = { | ||
name: "rebuild-logger", | ||
setup(build: esbuild.PluginBuild) { | ||
build.onStart(() => { | ||
console.log(`[${new Date().toISOString()}] Rebuilding JS files...`); | ||
}); | ||
}, | ||
}; | ||
|
||
bundle(); | ||
const metafilePlugin = { | ||
name: "metafile", | ||
setup(build: esbuild.PluginBuild) { | ||
build.onEnd((result) => { | ||
if (metafile) { | ||
fs.writeFileSync( | ||
"esbuild-metadata.json", | ||
JSON.stringify(result.metafile) | ||
); | ||
} | ||
}); | ||
}, | ||
}; | ||
|
||
esbuild | ||
.context({ | ||
entryPoints: { | ||
"dataframe/dataframe": "dataframe/index.tsx", | ||
"ml/ml": "ml/index.ts", | ||
}, | ||
format: "esm", | ||
bundle: true, | ||
outdir: "../shiny/www/shared", | ||
minify: minify, | ||
sourcemap: true, | ||
metafile: true, | ||
plugins: [ | ||
sassPlugin({ type: "css-text", sourceMap: false }), | ||
rebuildLoggerPlugin, | ||
metafilePlugin, | ||
], | ||
}) | ||
.then((context) => { | ||
if (watch) { | ||
context.watch(); | ||
} else { | ||
context.rebuild(); | ||
context.dispose(); | ||
} | ||
}) | ||
.catch(() => process.exit(1)); |
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,120 @@ | ||
import { LitElement, css, html } from "lit"; | ||
import { property } from "lit/decorators.js"; | ||
|
||
export class ShinyClassificationLabel extends LitElement { | ||
static styles = css` | ||
:host { | ||
display: block; | ||
padding-bottom: 2rem; | ||
} | ||
|
||
.wrapper { | ||
border: 1px solid #ccc; | ||
padding: 12px; | ||
border-radius: 5px; | ||
} | ||
|
||
.item { | ||
margin-top: 20px; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.winner { | ||
display: flex; | ||
justify-content: center; | ||
font-weight: bold; | ||
font-size: 1.75em; | ||
} | ||
|
||
.bar { | ||
height: 5px; | ||
margin-top: 5px; | ||
margin-bottom: 5px; | ||
background-color: #4890e3; | ||
border-radius: 2px; | ||
} | ||
|
||
.label { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: baseline; | ||
} | ||
|
||
.dashed-line { | ||
flex: 1 1 0%; | ||
border-bottom: 1px dashed #888; | ||
margin-left: 0.5em; | ||
margin-right: 0.5em; | ||
} | ||
Comment on lines
+6
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Were you planning to make these rules use CSS variables? And/or default to existing CSS variables? |
||
`; | ||
|
||
@property({ type: Object }) value: Record<string, number> = {}; | ||
@property({ type: Number }) sort: number = 1; | ||
@property({ type: Number, attribute: "display-winner" }) | ||
displayWinner: number = 0; | ||
@property({ type: Number, attribute: "max-items" }) | ||
maxItems: number | null = null; | ||
@property({ type: String, attribute: "suffix" }) suffix: string = "%"; | ||
|
||
render() { | ||
let entries = Object.entries(this.value); | ||
|
||
if (this.maxItems !== null) { | ||
entries = truncateEntries(entries, this.maxItems); | ||
} | ||
|
||
if (this.sort) { | ||
entries.sort((a, b) => b[1] - a[1]); | ||
} | ||
|
||
let winnerHTML = null; | ||
if (this.displayWinner) { | ||
// Entries might not be sorted, so we need to loop through again. | ||
const currentWinner = { name: "", value: -Infinity }; | ||
entries.forEach(([k, v]) => { | ||
if (v > currentWinner.value) { | ||
currentWinner.name = k; | ||
currentWinner.value = v; | ||
} | ||
}); | ||
winnerHTML = html`<div class="winner">${currentWinner.name}</div>`; | ||
} | ||
|
||
const valuesHtml = entries.map(([k, v]) => { | ||
return html`<div class="item"> | ||
<div class="bar" style="width: ${v}${this.suffix};"></div> | ||
<div class="label"> | ||
<div>${k}</div> | ||
<div class="dashed-line"></div> | ||
<div>${v}%</div> | ||
</div> | ||
</div>`; | ||
}); | ||
|
||
return html`<div class="wrapper">${winnerHTML} ${valuesHtml}</div> `; | ||
} | ||
} | ||
|
||
customElements.define("shiny-classification-label", ShinyClassificationLabel); | ||
|
||
function truncateEntries(entries: [string, number][], maxItems: number | null) { | ||
// Just the numeric values | ||
const values = entries.map(([_, v]) => v); | ||
values.sort().reverse(); | ||
|
||
const cutoffValue = values[maxItems - 1]; | ||
|
||
const newEntries = []; | ||
for (const entry of entries) { | ||
if (entry[1] >= cutoffValue) { | ||
newEntries.push(entry); | ||
// In case there are multiple items that match cutoffValue, we need to make sure | ||
// we don't add more entries than were asked for. | ||
if (newEntries.length === maxItems) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return newEntries; | ||
} |
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.
Was this on purpose?