Home > Blockchain >  Writing a website in streamlit, how can I avoid using long if-chains?
Writing a website in streamlit, how can I avoid using long if-chains?

Time:05-03

I'm writing a project for a class and I do not know how to not make the code as a long if chain, would it work to make it as a class instead then call that class through out the program?

import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

class religionss:
    def __init__(self, size, history, rituals, meditation, belief, scripts):
        self.size = size
        self.history = history
        self.rituals = rituals
        self.meditation = meditation
        self.belief = belief
        self.scripts = scripts

st.title('Världsreligionerna')

religion_options = st.selectbox(
    'Välj religion nedan: ',
    (f'Kristendomen', 'Islam', 'Judendomen',
     'Hinduismen', 'Buddhismen'))

st.write(f'Du valde: {religion_options}')

if religion_options == 'Islam':
    st.write('Islam är en abrahamitisk religion som grundades för ca 1400 år sedan.')
    box = st.selectbox('Välj kategori nedan:',
                       ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))
    if box == 'Storlek':
        st.write('1 miljard')

elif religion_options == 'Judendomen':
    box = st.selectbox('Välj kategori nedan:',
                       ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))

elif religion_options == 'Hinduismen':
    box = st.selectbox('Välj kategori nedan:',
                       ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))

elif religion_options == 'Buddhismen':
    box = st.selectbox('Välj kategori nedan:',
                       ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))

elif religion_options == 'Kristendomen':
    box = st.selectbox('Välj kategori nedan:',
                       ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))

CodePudding user response:

Here is an approach without using if/else. It uses a lookup through a dictionary.

The selectbox option is the same so I refactored it too.

Code

import streamlit as st
# import pandas as pd
# import numpy as np
# import matplotlib.pyplot as plt


def mybox():
    box = st.selectbox('Välj kategori nedan:',
                        ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))
    return box


def Islam():
    st.write('Islam är en abrahamitisk religion som grundades för ca 1400 år sedan.')
    box = mybox()

    if box == 'Storlek':
        st.write('1 miljard')


def Judendomen():
    box = mybox()


def Hinduismen():
    box = mybox()


def Buddhismen():
    box = mybox()


def Kristendomen():
    box = mybox()


def main():

    st.title('Världsreligionerna')

    religion_options = st.selectbox(
        'Välj religion nedan: ',
        (f'Kristendomen', 'Islam', 'Judendomen',
         'Hinduismen', 'Buddhismen'))

    st.write(f'Du valde: {religion_options}')

    opt_dict = {
        'Islam': Islam,
        'Judendomen': Judendomen,
        'Hinduismen': Hinduismen,
        'Buddhismen': Buddhismen,
        'Kristendomen': Kristendomen
    }

    opt_dict[religion_options]()


if __name__ == '__main__':
    main()

Output

Landing page

enter image description here

After selecting islam

enter image description here

Put it in a class

Code

"""
religion.py
"""


import streamlit as st
# import pandas as pd
# import numpy as np
# import matplotlib.pyplot as plt


class Religion:
    def __init__(self):
        pass

    def start_page(self):
        st.title('Världsreligionerna')

        religion_options = st.selectbox(
            'Välj religion nedan: ',
            (f'Kristendomen', 'Islam', 'Judendomen',
             'Hinduismen', 'Buddhismen'))

        st.write(f'Du valde: {religion_options}')

        opt_dict = {
            'Islam': Islam,
            'Judendomen': Judendomen,
            'Hinduismen': Hinduismen,
            'Buddhismen': Buddhismen,
            'Kristendomen': Kristendomen
        }
        opt_dict[religion_options]()



def mybox():
    box = st.selectbox('Välj kategori nedan:',
                        ('Storlek', 'Historia', 'Ritualer',
                        'Meditation', 'Tro', 'Heliga skrifter'))
    return box


def Islam():
    st.write('Islam är en abrahamitisk religion som grundades för ca 1400 år sedan.')
    box = mybox()

    if box == 'Storlek':
        st.write('1 miljard')


def Judendomen():
    box = mybox()


def Hinduismen():
    box = mybox()


def Buddhismen():
    box = mybox()


def Kristendomen():
    box = mybox()


def main():
    religion = Religion()
    religion.start_page()


if __name__ == '__main__':
    main()

You can use the module religion.py in another script lets say sample.py.

"""
sample.py
"""


import religion

var = religion.Religion()
var.start_page()
  • Related