Display Image as link using markdown

As below, I displayed images that have URLs using markdown.
I thought it would be right-aligned, but it was not.
Here is my code and Output image.
Please let me know what was wrong and how to fix it. :disappointed_relieved:

import os, base64
import streamlit as st
import numpy as np

st.set_page_config(layout="wide")

@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
    with open(bin_file, "rb") as f:
        data = f.read()
    return base64.b64encode(data).decode()

@st.cache(allow_output_mutation=True)
def get_img_with_href(local_img_path, target_url):
    img_format = os.path.splitext(local_img_path)[-1].replace(".", "")
    bin_str = get_base64_of_bin_file(local_img_path)
    html_code = f"""
    <a href="{target_url}">
            <img src="data:image/{img_format};base64,{bin_str}" style="width:100%; height:100%" />
        </a>"""
    return html_code

cols = st.columns([1]*12+[6])
imgDir = {local image directory}
imgLs = np.sort(os.listdir(imgDir))

for idx in range(len(imgLs)):
    gif_html = get_img_with_href(os.path.join(imgDir,imgLs[idx]), {URL})
    cols[idx%12].markdown(gif_html, unsafe_allow_html=True)  

Hi @Jameson, and welcome to the Streamlit community! :balloon::raised_hands:

That’s a good question!

I’ve not tried your code yet I’m wondering: would amending the st.columns parameters allow you to move the images to the right?

Charly

sorry to reply lately, and something was missing in my question.

First, this is what I want to show and code.

import os, base64
import streamlit as st
import numpy as np
import pandas as pd

st.set_page_config(layout="wide")

@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
    with open(bin_file, "rb") as f:
        data = f.read()
    return base64.b64encode(data).decode()

@st.cache(allow_output_mutation=True)
def get_img_with_href(local_img_path, target_url):
    img_format = os.path.splitext(local_img_path)[-1].replace(".", "")
    bin_str = get_base64_of_bin_file(local_img_path)
    html_code = f"""
    <a href="{target_url}">
            <img src="data:image/{img_format};base64,{bin_str}" style="width:100%; height:100%" />
        </a>"""
    return html_code

cols = st.columns([1]*12+[6])
imgDir = {imagePath}
imgLs = np.sort(os.listdir(imgDir))
data = {dataPath}

for idx in range(len(imgLs)):
    gif_html = get_img_with_href(os.path.join(imgDir,imgLs[idx]), "streamlit.io")
    cols[idx%12].markdown(gif_html, unsafe_allow_html=True)  

cols[12].dataframe(data)

So, I tried another code. But, If the dataframe row size is bigger, then the images are farther.

import os, base64
import streamlit as st
import numpy as np
import pandas as pd

st.set_page_config(layout="wide")

@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
    with open(bin_file, "rb") as f:
        data = f.read()
    return base64.b64encode(data).decode()

@st.cache(allow_output_mutation=True)
def get_img_with_href(local_img_path, target_url):
    img_format = os.path.splitext(local_img_path)[-1].replace(".", "")
    bin_str = get_base64_of_bin_file(local_img_path)
    html_code = f"""
    <a href="{target_url}">
            <img src="data:image/{img_format};base64,{bin_str}" style="width:100%; height:100%" />
        </a>"""
    return html_code

cols = st.columns([1]*12+[6])
imgDir = {imagePath}
imgLs = np.sort(os.listdir(imgDir))
data = {dataPath}

for i in range(4):
    cols = st.columns([1]*12+[6])
    for idx in range(12):        
        gif_html = get_img_with_href(os.path.join(imgDir,imgLs[idx]), "www.naver.com")
        cols[idx%12].markdown(gif_html, unsafe_allow_html=True)  
    cols[12].dataframe(data)

Hi @Jameson

Just to confirm, is this the layout you need to display?

image

Charly

cc: @snehankekre

HI @Charly_Wargnier

Definitely YES! :pray:
But as you can see, there is some crack