|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "nbsphinx": "hidden" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Vitessce Widget Tutorial" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 18, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from vitessce import (\n", |
| 19 | + " VitessceConfig,\n", |
| 20 | + " Component as cm,\n", |
| 21 | + " CoordinationType as ct,\n", |
| 22 | + " OmeTiffWrapper,\n", |
| 23 | + " MultiImageWrapper,\n", |
| 24 | + " VitesscePlugin\n", |
| 25 | + ")\n", |
| 26 | + "from esbuild_py import transform" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 40, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "PLUGIN_ESM = transform(\"\"\"\n", |
| 36 | + "function createPlugins(utilsForPlugins) {\n", |
| 37 | + " const {\n", |
| 38 | + " React,\n", |
| 39 | + " PluginFileType,\n", |
| 40 | + " PluginViewType,\n", |
| 41 | + " PluginCoordinationType,\n", |
| 42 | + " PluginJointFileType,\n", |
| 43 | + " z,\n", |
| 44 | + " useCoordination,\n", |
| 45 | + " invokeCommand,\n", |
| 46 | + " } = utilsForPlugins;\n", |
| 47 | + " \n", |
| 48 | + " const CSS = `\n", |
| 49 | + " .chat {\n", |
| 50 | + " overflow-y: scroll;\n", |
| 51 | + " }\n", |
| 52 | + " `;\n", |
| 53 | + " \n", |
| 54 | + " function ChatView(props) {\n", |
| 55 | + " \n", |
| 56 | + " const [nextMessage, setNextMessage] = React.useState('');\n", |
| 57 | + " const [isLoading, setIsLoading] = React.useState(false);\n", |
| 58 | + " const [chatHistory, setChatHistory] = React.useState([]); // chatHistory is an array of message objects like [{ user, text }, ...]\n", |
| 59 | + " \n", |
| 60 | + " async function handleClick() { \n", |
| 61 | + " setChatHistory(prev => ([\n", |
| 62 | + " ...prev,\n", |
| 63 | + " { user: 'You', text: nextMessage },\n", |
| 64 | + " ]));\n", |
| 65 | + " setIsLoading(true);\n", |
| 66 | + " const [chatReceiveValue, chatReceiveBuffers] = await invokeCommand(\"chat_send\", nextMessage, []);\n", |
| 67 | + " setChatHistory(prev => ([\n", |
| 68 | + " ...prev,\n", |
| 69 | + " { user: 'AI', text: chatReceiveValue.text },\n", |
| 70 | + " ]));\n", |
| 71 | + " setIsLoading(false);\n", |
| 72 | + " }\n", |
| 73 | + " \n", |
| 74 | + " return (\n", |
| 75 | + " <>\n", |
| 76 | + " <style>{CSS}</style>\n", |
| 77 | + " <div className=\"chat\">\n", |
| 78 | + " <p>Chat view</p>\n", |
| 79 | + " <div>\n", |
| 80 | + " {chatHistory.map(message => (\n", |
| 81 | + " <p key={`${message.user}-${message.text}`}>\n", |
| 82 | + " <b>{message.user}</b>:\n", |
| 83 | + " <span>{message.text}</span>\n", |
| 84 | + " </p>\n", |
| 85 | + " ))}\n", |
| 86 | + " </div>\n", |
| 87 | + " <input type=\"text\" value={nextMessage} onChange={e => setNextMessage(e.target.value)} disabled={isLoading} />\n", |
| 88 | + " <button onClick={handleClick}>Send message</button>\n", |
| 89 | + " </div>\n", |
| 90 | + " </>\n", |
| 91 | + " );\n", |
| 92 | + " }\n", |
| 93 | + "\n", |
| 94 | + " const pluginViewTypes = [\n", |
| 95 | + " new PluginViewType('chat', ChatView, []),\n", |
| 96 | + " ];\n", |
| 97 | + " return { pluginViewTypes };\n", |
| 98 | + "}\n", |
| 99 | + "export default { createPlugins };\n", |
| 100 | + "\"\"\")\n", |
| 101 | + "\n", |
| 102 | + "\n", |
| 103 | + "def handle_chat_message(message, buffers):\n", |
| 104 | + " return { \"text\": message.upper() }, []\n", |
| 105 | + "\n", |
| 106 | + "\n", |
| 107 | + "class ChatPlugin(VitesscePlugin):\n", |
| 108 | + " plugin_esm = PLUGIN_ESM\n", |
| 109 | + " commands = {\n", |
| 110 | + " \"chat_send\": handle_chat_message,\n", |
| 111 | + " }" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 41, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "vc = VitessceConfig(schema_version=\"1.0.15\", name='Spraggins Multi-Modal', description='PAS + IMS + AF From https://portal.hubmapconsortium.org/browse/collection/6a6efd0c1a2681dc7d2faab8e4ab0bca')\n", |
| 121 | + "dataset = vc.add_dataset(name='Spraggins').add_object(\n", |
| 122 | + " MultiImageWrapper(\n", |
| 123 | + " image_wrappers=[\n", |
| 124 | + " OmeTiffWrapper(img_url='https://assets.hubmapconsortium.org/f4188a148e4c759092d19369d310883b/ometiff-pyramids/processedMicroscopy/VAN0006-LK-2-85-PAS_images/VAN0006-LK-2-85-PAS_registered.ome.tif?token=', name='PAS'),\n", |
| 125 | + " OmeTiffWrapper(img_url='https://assets.hubmapconsortium.org/2130d5f91ce61d7157a42c0497b06de8/ometiff-pyramids/processedMicroscopy/VAN0006-LK-2-85-AF_preIMS_images/VAN0006-LK-2-85-AF_preIMS_registered.ome.tif?token=', name='AF'),\n", |
| 126 | + " OmeTiffWrapper(img_url='https://assets.hubmapconsortium.org/be503a021ed910c0918842e318e6efa2/ometiff-pyramids/ometiffs/VAN0006-LK-2-85-IMS_PosMode_multilayer.ome.tif?token=', name='IMS Pos Mode'),\n", |
| 127 | + " OmeTiffWrapper(img_url='https://assets.hubmapconsortium.org/ca886a630b2038997a4cfbbf4abfd283/ometiff-pyramids/ometiffs/VAN0006-LK-2-85-IMS_NegMode_multilayer.ome.tif?token=', name='IMS Neg Mode')\n", |
| 128 | + " ],\n", |
| 129 | + " use_physical_size_scaling=True,\n", |
| 130 | + " )\n", |
| 131 | + ")\n", |
| 132 | + "spatial = vc.add_view(cm.SPATIAL, dataset=dataset)\n", |
| 133 | + "status = vc.add_view(\"chat\", dataset=dataset)\n", |
| 134 | + "lc = vc.add_view(cm.LAYER_CONTROLLER, dataset=dataset).set_props(disableChannelsIfRgbDetected=True)\n", |
| 135 | + "vc.layout(spatial | (lc / status));" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "## 2. Create the Vitessce widget" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 42, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "data": { |
| 152 | + "application/vnd.jupyter.widget-view+json": { |
| 153 | + "model_id": "52bbdb1e3f91423b8dd934e3a4ff796e", |
| 154 | + "version_major": 2, |
| 155 | + "version_minor": 1 |
| 156 | + }, |
| 157 | + "text/plain": [ |
| 158 | + "VitessceWidget(config={'version': '1.0.15', 'name': 'Spraggins Multi-Modal', 'description': 'PAS + IMS + AF Fr…" |
| 159 | + ] |
| 160 | + }, |
| 161 | + "execution_count": 42, |
| 162 | + "metadata": {}, |
| 163 | + "output_type": "execute_result" |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "vw = vc.widget(plugins=[ChatPlugin()])\n", |
| 168 | + "vw" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": null, |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [], |
| 183 | + "source": [] |
| 184 | + } |
| 185 | + ], |
| 186 | + "metadata": { |
| 187 | + "kernelspec": { |
| 188 | + "display_name": "Python 3 (ipykernel)", |
| 189 | + "language": "python", |
| 190 | + "name": "python3" |
| 191 | + }, |
| 192 | + "language_info": { |
| 193 | + "codemirror_mode": { |
| 194 | + "name": "ipython", |
| 195 | + "version": 3 |
| 196 | + }, |
| 197 | + "file_extension": ".py", |
| 198 | + "mimetype": "text/x-python", |
| 199 | + "name": "python", |
| 200 | + "nbconvert_exporter": "python", |
| 201 | + "pygments_lexer": "ipython3", |
| 202 | + "version": "3.9.0" |
| 203 | + } |
| 204 | + }, |
| 205 | + "nbformat": 4, |
| 206 | + "nbformat_minor": 4 |
| 207 | +} |
0 commit comments