Import Shiny Python functions doesn't work

I want to create a Shiny Python app without success for image detection. But the functions in shiny library don't work. My example is:

# Run in Jupyter notebook inside a conda environment where:
# pip install ultralytics
# pip install shiny

import shiny
from shiny import fluidPage, titlePanel, sidebarLayout, sidebarPanel, mainPanel, fileInput, textOutput, plotOutput
import numpy as np
from PIL import Image
import torch
from torchvision.transforms import functional as F
import ultralytics as yolo

# Load the model
def load_model():
    model = yolo.YOLO("")
    return model
model = load_model()

# Define the UI
ui = fluidPage(
    # App title
    titlePanel("Hello YOLOv8!"),
    # Sidebar layout with input and output definitions
        # Sidebar panel for inputs
            # Input: File upload
            fileInput("image_path", label = "Input a JPEG image")
        # Main panel for displaying outputs
            # Output: Text
            textOutput(outputId = "prediction"),
            # Output: Plot
            plotOutput(outputId = "image")

# Define server logic required to make predictions
def server(input, output):
    def update_image(change):
            # Convert uploaded image to a numpy array
            img =
            img = np.array(img)

            # Preprocess image
            img = F.to_tensor(F.resize(img, (416, 416))).unsqueeze(0)

            # Use the loaded model to make predictions
            prediction = model(img)

            # Convert prediction to text
            output.prediction.value = "The predicted class is {}".format(prediction)

            # Display image
   = img

# Create Shiny app
app = shiny.Shiny(ui = ui, server = server)

# Run the app

#ImportError: cannot import name 'fluidPage' from 'shiny' 

Please, any help with it?

The names of your shiny functions are mostly wrong. Check out the different shiny functions in Shiny for Python. All of the ui functions can be loaded by doing from shiny import ui. There is no fluidPage function. There is ui.page_fluid. There's no titlePanel function. There's ui.panel_title. Pretty much all of the ui functions you are loading have the names from R shiny, not python shiny. That link above should help you use the right functions.

1 Like

Note that chatGPT doesn't do a very good job at generating Shiny for Python code on its own because it halucinates python functions that look like their R equivalent. The best solution I've found for this is to use cursor and use a documentation embedding of the shiny for python website.