How to link a local file or directory by st.markdown?

I want to create a link, when I click it , chrome or firefox can open a new page and show content.
I know following way works.

st.markdown('[url_link](https://www.google.com/)')

but when I use a local file path instead a url, it doesnt work. like:

st.markdown('[url_link](file:///home/)')

or

st.markdown('[url_link](file:///home/test.log)')

Do you know how to fix it?
Thanks !

Unfortunately, that will not work for a local file sitting in your filesystem, unless it is being served by a server, something like python -m http.server.

Depending on what exactly what you’re trying to do, you might be able to use st.download_button instead to allow the user to download a local file, or simple write the file contents into your app

from pathlib import Path

import streamlit as st

st.write(Path("/home/test.log").read_text())

st.download_button(
    "Download file", Path("/home/test.log").read_text(), "test.log"
)

That’s OK, thanks anyway!

You could use a button to display a local file in the browser. Not as elegant as a hyperlink, but it does the job.
Use the button to trigger the python check_output() function, which can be used to execute a shell commands.
The Windows shell command to execute the default action for a file is: start .

from subprocess import check_output

if st.button('Show Html file'):
	file_name = 'index.html'
	check_output("start " + file_name, shell=True)

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