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("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")
    return model
  
model = load_model()

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

# Define server logic required to make predictions
def server(input, output):
    @input.on_change('image_path')
    def update_image(change):
        if change.new:
            # Convert uploaded image to a numpy array
            img = Image.open(change.new)
            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
            output.image.data = img

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

# Run the app
app.run_server()

#ImportError: cannot import name 'fluidPage' from 'shiny' 
#(/home/fores/anaconda3/envs/myyolov8/lib/python3.12/site-packages/shiny/__init__.py)

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. https://cursor.sh/

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.