Git Product home page Git Product logo

st-card's Introduction

st-card

Streamlit Component, for a UI card

authors - @gamcoh @Pernod Ricard

image

Installation

Install streamlit-card with pip

pip install streamlit-card

usage, import the card function from streamlit_card

from streamlit_card import card

hasClicked = card(
  title="Hello World!",
  text="Some description",
  image="http://placekitten.com/200/300",
  url="https://github.com/gamcoh/st-card"
)

You can also use a local image by doing this instead

import base64

with open(filepath, "rb") as f:
    data = f.read()
    encoded = base64.b64encode(data)
data = "data:image/png;base64," + encoded.decode("utf-8")

from streamlit_card import card

hasClicked = card(
  title="Hello World!",
  text="Some description",
  image=data
  url="https://github.com/gamcoh/st-card"
)

You can also create a card without an URL. That way you control the behavior when the user click on it. For instance:

from streamlit_card import card

hasClicked = card(
  title="Hello World!",
  text="Some description",
  image="http://placekitten.com/200/300",
)

if hasClicked:
    # do something

If you want, you could use a callback to handle the click like so:

from streamlit_card import card

hasClicked = card(
  title="Hello World!",
  text="Some description",
  image="http://placekitten.com/200/300",
  on_click=lambda: print("Clicked!")
)

Customizations

If you want, you could use a callback to handle the click like so:

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text="This is a test card",
    image="https://placekitten.com/500/500",
    styles={
        "card": {
            "width": "500px",
            "height": "500px",
            "border-radius": "60px",
            "box-shadow": "0 0 10px rgba(0,0,0,0.5)",
            ...
        },
        "text": {
            "font-family": "serif",
            ...
        }
    }
)

If you want to set the size of as use_column_width=True, do this:

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text="This is a test card",
    image="https://placekitten.com/500/500",
    styles={
        "card": {
            "width": "100%", # <- make the card use the width of its container, note that it will not resize the height of the card automatically
            "height": "300px" # <- if you want to set the card height to 300px
            ...
        }
    }
)

If you want to modify the filter applied to the image, you could do this:

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text="This is a test card",
    image="https://placekitten.com/500/500",
    styles={
        "card": {
            "width": "500px",
            "height": "500px",
            "border-radius": "60px",
            "box-shadow": "0 0 10px rgba(0,0,0,0.5)",
            ...
        },
        "filter": {
            "background-color": "rgba(0, 0, 0, 0)"  # <- make the image not dimmed anymore
            ...
        }
    }
)

The editable CSS are "card", "title", "text", "filter" and "div".

Multiple descriptions

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text=["This is a test card", "This is a subtext"],
    image="https://placekitten.com/500/500",
)

st-card's People

Contributors

aliyevorkhan avatar benschza avatar blackary avatar ethanknights avatar gamcoh avatar jung-hunsoo avatar ojddjo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

st-card's Issues

Click on multiple cards

Hello,

Thank you for providing the card feature!

When using multiple cards, how can I get click results for each card?
For instance, with the following code:

hasClicked1 = card(
    title="111",
    text="Some description",
    image="http://placekitten.com/200/300",
    key="111",
    on_click=lambda: print("111"),
)
hasClicked2 = card(
    title="222",
    text="Some description",
    image="http://placekitten.com/200/300",
    key="222",
    on_click=lambda: print("222"),
)
print(f"{hasClicked1} {hasClicked2}")

When 111 is clicked, the result is:

True False  # print

And click 222, the result is:

True True  # print ...?

Since I only clicked on the "222" card,
I hoped that "False True" would come out, but it didn't work as expected.

How can i fix it?

Thank you in advance for your response!

Which type of image can I use for "image" attribute?

Hi authors,

I'm using your package but cannot change any local image instead of your's example. Specifically, I have tried read some type of image such as .jpg and .png by using PIL.Image, but then it shows an error of cannot serialize as bellow

MarshallComponentException: ('Could not convert component args to JSON', TypeError('Object of type PngImageFile is not JSON serializable'))

How can I fix this

Feature - Option to call a function when clicked.

I think having the ability to call a function when clicked would be a great feature. This would allow you to use the streamlit-extras switch page component for example when used in a function. Could add this feature as optional and if the URL argument is None then it would resort to the function call if present.

Card without image gives error, support colored bg instead of image

The documentation says that the image parameter is optional, but if I don't pass it, I get warning messages:

2024-01-03 21:56:32.558 ComponentRequestHandler: GET /Users/preining/.pyenv/versions/3.10.13/envs/ffe-eval-dash2/lib/python3.10/site-packages/streamlit_card/frontend/build/null read error
Traceback (most recent call last):
  File "/Users/preining/.pyenv/versions/3.10.13/envs/ffe-eval-dash2/lib/python3.10/site-packages/streamlit/web/server/component_request_handler.py", line 55, in get
    with open(abspath, "rb") as file:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/preining/.pyenv/versions/3.10.13/envs/ffe-eval-dash2/lib/python3.10/site-packages/streamlit_card/frontend/build/null'

In fact, what I would like to see is that one can just give a color for the background, and no image is needed.

Is this possible?

Thanks for providing this widget!

Can't update title CSS styling

The package documentation says the "title" is also CSS editable but when I try to edit, nothing happens.

The following example works for "text" but updates all the text in the card.

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text="This is a test card",
    image="https://placekitten.com/500/500",
    styles={
        "card": {
            "width": "500px",
            "height": "500px",
            "border-radius": "60px",
            "box-shadow": "0 0 10px rgba(0,0,0,0.5)",
            
        },
        "text" : {
            "color" : "red"
        }
    }
)

image

When I try to update "text" to title, the font color is white and nothing changes.

from streamlit_card import card

res = card(
    title="Streamlit Card",
    text="This is a test card",
    image="https://placekitten.com/500/500",
    styles={
        "card": {
            "width": "500px",
            "height": "500px",
            "border-radius": "60px",
            "box-shadow": "0 0 10px rgba(0,0,0,0.5)",
            
        },
        "title" : {
            "color" : "red"
        }
    }
)

image

I'm using streamlit-card==0.0.61 and Python 3.9.13.

Any ideas why?

Update Version

Hey @gamcoh ,

can you update your package to make it compatible with Python Version > 3.7.8?

Best regards
Chris

Multiple On Clicks run when I click on only one.

Hi @gamcoh,

Thanks for making this package I like it a lot.

I ran into an issue with using the on_click feature of the card.

from streamlit_card import card

import base64

with open('transparent.png', "rb") as f:
    data = f.read()
    encoded = base64.b64encode(data)
data = "data:image/png;base64," + encoded.decode("utf-8")

from streamlit_card import card

hasClicked = card(
  title="Button 1",
  text="Some description",
  image=data,
  on_click=lambda: print('hi'),
  key='first'
)

hasClicked2 = card(
  title="Button 2",
  text="Some description",
  image=data,
  on_click=lambda: print('hi again'),
  key='second'
)

Here is a quick example that reproduces the issue I am having.

When you click on the card that is titled "Button 1" it will print "hi".

Afterwards if you then click on the card titled "Button 2" instead of expected output to be "hi again" it will instead print "hi" then "hi again".

I believe it has something to do with the __init__.py file checking

if clicked:
    on_click()

Which after clicking on a card it is set to true, but also stays true when pressing another card resulting in both on_clicks to execute.

Thanks again for making this super useful just struggling with figuring out how exactly the on_click portion works.

key argument

what key argument does it take and more about it please help me out

card size

How can I modify size of the card?

Questions about custiomization

I have 2 questions about customization.

  1. How to make many card in one line (like columns), for example 3?
  2. How to change card location? To left, right, etc.

Key remains True after app rerun

Issue:

When you click the card on, the varible will turn true. However it remains true even after a re run of the app.

Scenario:

I have 2 cards in my app. I have an onclick function that prints the name of the card when clicked. When I click the first card I return the title 'Card 1'. The app then re runs as all streamlit apps do. After the rerun I click the second card and the app prints 'Card 1' and 'Card 2' because in the statefulness of the cards they are both considered true.

Solution.

The statefulness of the cards gets reset to false on app re runs.

Example:

        s1 = card(
                        title=1,
                        text="Card 1",
                        image="http://placekitten.com/200/300",
                        on_click=lambda: print("s1!"),
                        key = 's1')
        
        s2 = card(
                        title=2,
                        text="Card 2",
                        image="http://placekitten.com/200/300",
                        on_click=lambda: print("s2!"),
                        key = 's2')

Cards overlapping with changing window size

When adjusting the window size of my streamlit app that uses st-card. I am running into an issue where the cards will start to overlap each other. Is there a work around for this?

Thanks st-card has been super useful.

Create the registration phase: step #1

  • [front] Email, full name, pass, pass confirmation, sex
  • [api] check if the email already exists
    • [front] redirect to login with notification/alert
  • [api] check if the form data are correct
  • [api] log in the attempt even if the info are not correct
    • [db] create a new database
  • [api] confirm email address by sending a code
    • use a email service

Multiple card on click behaviour

I have multiple cards, roughly creating by -

for my_card in enumerate(my_cards):
     card(**my_card)

Now I want to add a functionality that when I click them it writes down something, let's say the title, e.g st.write(my_card['title'])

When adding something like -

for my_card in enumerate(my_cards):
     card(**my_card, on_click=lambda: st.write(my_card))

It writes it for all the cards that were previously clicked. Is there a way to disable previous clicks / identify the last clicked card? or any other option that will allow me to control the behavior?

hi, i got this error mesage when using the package TypeError: 'NoneType' object is not callable

hi, i got this error mesage when using the package

TypeError: 'NoneType' object is not callable
Traceback:
File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
exec(code, module.dict)
File "/app/app.py", line 19, in
hasClicked = card(
File "/usr/local/lib/python3.10/site-packages/streamlit_card/init.py", line 54, in card
on_click()

how could i config my nginx

image

my nginx.conf

server {
    listen       8000;
    server_name  localhost;

   location = /hsk {
         proxy_pass http://127.0.0.1:8501;
    }

    location ^~ /static {
        proxy_pass http://127.0.0.1:8501/static/;
    }

    location = /_stcore/health {
        proxy_pass http://127.0.0.1:8501/_stcore/health;
    }

  location = /_stcore/host-config {
        proxy_pass http://127.0.0.1:8501/_stcore/host-config;
    }

    location = /hsk/_stcore/health {
        proxy_pass http://127.0.0.1:8501/_stcore/health;
    }

    location = /hsk/_stcore/host-config {
        proxy_pass http://127.0.0.1:8501/_stcore/host-config;
    }

    location = /_stcore/allowed-message-origins {
        proxy_pass http://127.0.0.1:8501/_stcore/allowed-message-origins;
    }

    location = /hsk/_stcore/stream {
        proxy_pass http://127.0.0.1:8501/_stcore/stream;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline';" always;

@gamcoh pls

Need some customization

I need to customize font size, font family and multi line description. As well as the card width and height.

Frontend build folder missing

Hi,

it seems like the frontend build folder is missing when installing with pip. Running a streamlit app with card components throws this error:

FileNotFoundError: [Errno 2] No such file or directory: ...\\.conda\\envs\\...\\Lib\\site-packages\\streamlit_card\\frontend\\build\\null'
ComponentRequestHandler: GET ...\.conda\envs\...\Lib\site-packages\streamlit_card\frontend\build\null read error

I once had a similar issue with this streamlit component (issue).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.