Skip to content

Instantly share code, notes, and snippets.

@pplonski
Created June 9, 2023 08:23
Show Gist options
  • Save pplonski/4f47dbac6068729449e89a470beef862 to your computer and use it in GitHub Desktop.
Save pplonski/4f47dbac6068729449e89a470beef862 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d2af7c7c",
"metadata": {},
"outputs": [],
"source": [
"import mercury as mr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cf907f71",
"metadata": {},
"outputs": [],
"source": [
"def some_api(capital, text):\n",
"\n",
" if capital == \"small\":\n",
" text = text.lower()\n",
" else:\n",
" text = text.upper()\n",
" \n",
" return list(set(text))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6843eddf",
"metadata": {},
"outputs": [
{
"data": {
"application/mercury+json": "{\n \"widget\": \"App\",\n \"title\": \"Update on fly\",\n \"description\": \"Example code\",\n \"show_code\": false,\n \"show_prompt\": false,\n \"output\": \"app\",\n \"schedule\": \"\",\n \"notify\": \"{}\",\n \"continuous_update\": true,\n \"static_notebook\": false,\n \"show_sidebar\": true,\n \"full_screen\": true,\n \"allow_download\": true,\n \"model_id\": \"mercury-app\",\n \"code_uid\": \"App.0.40.24.1-rand84060fa2\"\n}",
"text/html": [
"<h3>Mercury Application</h3><small>This output won't appear in the web app.</small>"
],
"text/plain": [
"mercury.App"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"app = mr.App(title=\"Update on fly\", description=\"Example code\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b7bae00a",
"metadata": {},
"outputs": [
{
"data": {
"application/mercury+json": "{\n \"widget\": \"Select\",\n \"value\": \"small\",\n \"choices\": [\n \"small\",\n \"capital\"\n ],\n \"label\": \"Select capitalization\",\n \"model_id\": \"dfcfeb081bdf4d318bb876ac626c43c0\",\n \"code_uid\": \"Select.0.40.16.1-rand6540539c\",\n \"url_key\": \"\",\n \"disabled\": false,\n \"hidden\": false\n}",
"application/vnd.jupyter.widget-view+json": {
"model_id": "dfcfeb081bdf4d318bb876ac626c43c0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"mercury.Select"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"letter_size = mr.Select(label=\"Select capitalization\", \n",
" value=\"small\", choices=[\"small\", \"capital\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "69ad5596",
"metadata": {},
"outputs": [
{
"data": {
"application/mercury+json": "{\n \"widget\": \"Text\",\n \"value\": \"\",\n \"rows\": 1,\n \"label\": \"Provide some text\",\n \"model_id\": \"b127f36f164e4e28b3df46fa0f4660cc\",\n \"code_uid\": \"Text.0.40.15.1-rand719fd42d\",\n \"url_key\": \"\",\n \"disabled\": false,\n \"hidden\": false\n}",
"application/vnd.jupyter.widget-view+json": {
"model_id": "b127f36f164e4e28b3df46fa0f4660cc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"mercury.Text"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"text = mr.Text(label=\"Provide some text\", value=\"\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6053d90b",
"metadata": {},
"outputs": [
{
"data": {
"application/mercury+json": "{\n \"widget\": \"Button\",\n \"label\": \"Run API\",\n \"style\": \"primary\",\n \"value\": false,\n \"model_id\": \"ef540d0003d947c19b953fa6220d5904\",\n \"code_uid\": \"Button.0.40.11.1-rand1753341a\",\n \"disabled\": false,\n \"hidden\": false\n}",
"application/vnd.jupyter.widget-view+json": {
"model_id": "ef540d0003d947c19b953fa6220d5904",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"mercury.Button"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"run_api = mr.Button(label=\"Run API\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "462bb25c",
"metadata": {},
"outputs": [],
"source": [
"my_letters = []"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b4f75966",
"metadata": {},
"outputs": [],
"source": [
"if run_api.clicked:\n",
" my_letters = some_api(letter_size.value, text.value)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "13b6bbd6",
"metadata": {},
"outputs": [
{
"data": {
"application/mercury+json": "{\n \"widget\": \"Select\",\n \"value\": null,\n \"choices\": [],\n \"label\": \"Select letter\",\n \"model_id\": \"0e47ac5c3bcf4d40a5ea71f3314ed838\",\n \"code_uid\": \"Select.0.40.16.1-randee0eefed\",\n \"url_key\": \"\",\n \"disabled\": false,\n \"hidden\": false\n}",
"application/vnd.jupyter.widget-view+json": {
"model_id": "0e47ac5c3bcf4d40a5ea71f3314ed838",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"mercury.Select"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"selected_letter = mr.Select(label=\"Select letter\", choices=my_letters)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9929f62e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Selected letter: None\n"
]
}
],
"source": [
"print(f\"Selected letter: {selected_letter.value}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7817c13",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "menv",
"language": "python",
"name": "menv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@gmouawad
Copy link

gmouawad commented Jun 9, 2023

hello, if I may add an issue faced, if selected_letter has multiple items, directly, item1 is selected. and if I selected Item2, it refreshed and the print(f"Selected letter: {selected_letter.value}") returns "Selected letter: none". is that normal behavior? can we keep the items in the select object until Run API button is clicked again?

@pplonski
Copy link
Author

pplonski commented Jun 9, 2023

Sure we can. You can add varoable to keep selected values and only change it when button clicked.

@gmouawad
Copy link

gmouawad commented Jun 9, 2023

Sure we can. You can add varoable to keep selected values and only change it when button clicked.

can you let me know how? I tried applying a variable assignment but it didn't do the job

@pplonski
Copy link
Author

pplonski commented Jun 9, 2023

Please just move the cell with my_letters = [] to the top.

Cells are automatically re-executed after widget update. But we don't re-execute all cells, only cells below updated widget.

Screenshot 2023-06-09 at 14 59 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment