41. Shiny-Python Basics#

From inside VSCode, in a terminal you can install with the following commands

conda create -n shiny python=3.12
conda activate shiny
  • Then install shiny in the environment using conda as explained in the install doc.

pip install shiny

You can use the code below to check the socket name and the IP address of your machine. This is not really needed.

import os
import socket

print('host name', socket.gethostname())
print('host ip adddress', socket.gethostbyname(socket.gethostname()))
host name algobay.scu.edu
host ip adddress 129.210.116.89

41.1. Creating the default app#

  • The following code will create a test app automatically.

  • The program file is always named app.py

  • The code below is created in a folder called my_app. Any folder name is acceptable, but the program file name is always the same. This is how Shiny recognizes the app.

!shiny create
zsh:1: command not found: shiny

This gives some basic apps, for example, see:

from shiny import render, ui
from shiny.express import input

ui.panel_title("Hello Shiny!")
ui.input_slider("n", "N", 0, 100, 20)

@render.text
def txt():
    return f"n*2 is {input.n() * 2}"

41.2. Run the app and deploy to a URL#

Simply run the code below.

! shiny run --reload my_app/app.py
INFO:     Will watch for changes in these directories: ['/content/drive/MyDrive/Books_Writings/NLPBook/my_app']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [15421] using WatchFiles
INFO:     Started server process [15437]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [15437]
INFO:     Stopping reloader process [15421]

41.3. Documentation#

41.4. Chatbot#

  • To create a chatbot, see this repo: wch/chatstream

  • You can then modify the code in the various app.py files in the examples folder: wch/chatstream (which you can look at on Github. )