Spaces:
Sleeping
Sleeping
File size: 2,850 Bytes
1c89c80 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "8cd1e865-53d5-460b-8bae-5658e3aa3d16",
"metadata": {},
"outputs": [],
"source": [
"import panel as pn\n",
"pn.extension()\n",
"import requests\n",
"import random\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca65cc07-8181-4259-8770-9c780621eb78",
"metadata": {},
"outputs": [],
"source": [
"# Button widget\n",
"run_button = pn.widgets.Button(name=\"Click to get a new image\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8480d2c2-06c0-441e-9cf9-f858f2db1ec9",
"metadata": {},
"outputs": [],
"source": [
"def get_image(_):\n",
" \"\"\"\n",
" a function to generate random cat images\n",
" \"\"\"\n",
" api_url = 'https://api.thecatapi.com/v1/images/search'\n",
" img_url = requests.get(api_url, stream=True).json()[0]['url']\n",
" image = Image.open(requests.get(img_url, stream=True).raw)\n",
" return pn.pane.Image(image, sizing_mode='scale_width')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6fd5a63f-012a-419c-8386-22b5b8ff243f",
"metadata": {},
"outputs": [],
"source": [
"# Bind the get_image function with the button widget\n",
"interactive = pn.bind(get_image, run_button)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e829400-320f-437c-8465-ed392afe3aa9",
"metadata": {},
"outputs": [],
"source": [
"desc = pn.pane.Markdown(\"\"\"\n",
"# Hi there!\n",
"Thanks for trying out the Panel template. This is a super simple demo that combines a function with a widget. \n",
"Click the button below to generate a new cat image and check out https://panel.holoviz.org/\n",
"to learn more.\n",
"\"\"\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84bfdf52-b6e0-400d-baaf-d16303a72b65",
"metadata": {},
"outputs": [],
"source": [
"# Layout using Template\n",
"template = pn.template.FastListTemplate(\n",
" title='Generate random cat images', \n",
" sidebar=[desc, run_button],\n",
" main=[interactive],\n",
" accent_base_color=\"#88d8b0\",\n",
" header_background=\"#88d8b0\",\n",
")\n",
"template.servable()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|