Bloody Noobquestion: Get variable for beta expander headline and how to use spaces and implement url

Hey guys,

I am super new to python at all and a guy from my company showed me streamlit if i want to have a fast progress when it comes to a kind of gui. I hardly know the basics but for some small scripts it already works. However I am having 3 small questions for the following code:

   if single_file:
    df = pd.read_excel(single_file, header=None)
    st.write('Folgende Datei wurde ausgewählt:')
    st.dataframe(df)
with st.beta_expander ("Basisdaten zum Projekt"):
if single_file:
    #Auftragsnummer/Projektnummer/Auftraggeber
    projekt=(df.iat[0,1])
    Auftragsnummer=projekt.split("-")[0]
    Projektnummer=projekt.split("-")[1]
    st.write('Auftragsnummer: ',Auftragsnummer)
    st.write('Projektnummer: ',Projektnummer)
    auftraggeber=(df.iat[2,1])
    st.write('Auftraggeber:', auftraggeber)

Question 1: Can I add a variable to the with st.beta_expander (“Basisdaten zum Projekt”): , so that just right after the string “Basisdaten zum Projekt” the user would get the variable Projektnummer and Auftragsnummer as a string ?

Question 2:
I d like to write st.write('Auftragsnummer: ',Auftragsnummer) and st.write('Projektnummer: ',Projektnummer) next to each other, separated by spaces. Unfortunately spaces are ignored. How can I add space between text. How can i format text generally ? Is that possible ?

Question 3: I want to write a text and create a hyperlink. How can I do that? For example:

st.write(‘this is a link to an external url, click me’) now the urlk is www.internet.com (for example) . How can i do that ? :slight_smile: I hope u guys can help me. I really d like to learn more about writing little programs but its hard being a noob :frowning:

Ha @maow,

welcome to streamlit and the community :wave:
No worries, everyone has to start as a beginner somewhere. The important thing is to keep on learning even though it might be frustrating from time to time :slight_smile:

Regarding your questions 1 & 3 :

In python (from 3.xx on) (and also in the st.beta_expander function) you can simply combine strings by using the “+”. But since your Auftragsnummer most likely is no string but an integer, you have to convert it first. You can simply do that by using str().

To create a hyperlink, you can use st.markdown()

So in your case this would be:

import streamlit as st

Auftragsnummer = 1
with st.beta_expander("Basisdaten zum Projekt " + str(Auftragsnummer)):
     link = '[internet](http://internet.com)'
     st.markdown(link, unsafe_allow_html=True)

Is that how you need it?

Regarding your second question:

You can use st.markdown to use markdown syntax (which will help you finding results when googling).
E.g. you can try something that is suggested here :

For example:

st.markdown('''Start            End ''')

Best,
Alex

1 Like

Hallo and thanks for the answer so far. Unfortunately it looks like the maarkdown doesnt want to work out :frowning: :“TypeError: can only concatenate str (not “DeltaGenerator”) to str”.

I used different indents but so far… still have the same error :frowning: concerning the link…:

The code looks like that at the moment:

if single_file:

    df = pd.read_excel(single_file, header=None)

    st.write('Folgende Datei wurde ausgewählt:')

    st.dataframe(df)

    projekt=(df.iat[0,1])

    Auftragsnummer=projekt.split("-")[0]

    Projektnummer=projekt.split("-")[1]

else:

    Auftragsnummer=""

    Projektnummer=""

    projekt=""

leer=st.markdown("    ")

with st.beta_expander (“Basisdaten zum Projekt” + str(Auftragsnummer)+leer+str(Projektnummer)):

link = '[internet](http://internet.com)'

st.markdown(link, unsafe_allow_html=True)

    if single_file:

    #Auftragsnummer/Projektnummer/Auftraggeber

        st.write('Auftragsnummer: ',Auftragsnummer) 

        st.write('Projektnummer: ',Projektnummer)

        auftraggeber=(df.iat[2,1])

        st.write('Auftraggeber:', auftraggeber)

    else:

        st.write('Zur Darstellung der Basisdaten bitte zunächst eine Excel-Datei importieren!')

I want to add (concerning the link). I want it so that I just click on a word and the link opens; e.g.

receipts + click on it opens a webpage for receipts for cooking . the link itsselve is not needed to be written (just works in the background)

I found out the variable link should not be within the expander environment. It has to come before, otherwise it d create the problem with the intends. I dont know why but now the code looks like this and the problem with the Link is solved i guess. just still have the problem with the markdown :frowning:

link = ‘internet

with st.beta_expander (“Basisdaten zum Projekt” + str(Auftragsnummer)+str(Projektnummer)):

    st.markdown(link, unsafe_allow_html=True)

    if single_file:

    #Auftragsnummer/Projektnummer/Auftraggeber

        st.write('Auftragsnummer: ',Auftragsnummer) 

        st.write('Projektnummer: ',Projektnummer)

        auftraggeber=(df.iat[2,1])

        st.write('Auftraggeber:', auftraggeber)

    else:

        st.write('Zur Darstellung der Basisdaten bitte zunächst eine Excel-Datei importieren!')

        st.write('1 + 1 = ', 2)

Unfortunately, when using markup with the expression unsafe_allow_html=True the code wont work and i get an error “TypeError: can only concatenate str (not “DeltaGenerator”) to str”.

@ st.write('DepV (2009) wird zzgl. des '+st.markdown(erlass,unsafe_allow_html=True)+" berücksichtigt.")

the code which works looks like this… I dont know why markdown is not welcomed :frowning:

erlass=‘Erlasses

with st.beta_expander (“Optionen”):

st.subheader('Allgemeine Optionen')

option = st.radio("",('inklusive DepV', 'exklusive DepV'))

st.markdown('**Ausgewählte Option:**')

if option == 'inklusive DepV':

     st.write('DepV (2009) wird zzgl. des '+erlass+" berücksichtigt.")
    #st.markdown(erlass, unsafe_allow_html=True)

else:

    st.write("Es erfolgt lediglich die Einstufung nach LAGA (2004).")

    check_depV=1