Home > OS >  How to call python function/method in if else
How to call python function/method in if else

Time:09-01

I have been trying to call function to menu if condition is true. But not getting the desired result till now.

import streamlit as st 

class Gdrive:
    def gdrivee():
        # some code

    def local():
        # some code


options = {
    'gdrive': 'upload from google drive',
    'local': 'upload from computer'
}

selected_option = st.radio('Select page', options.values())

if selected_option == options['gdrive']:
    Gdrive.gdrivee()    # here i am trying to call one function


elif selected_option == options['local']:
    Gdrive.local()      # here trying to call function local 

In the above code, I am trying to call function gdrivee() if chosen option is upload from google drive and call function local() if chosen option is upload from computer If you guys know how to make it work, please do share. would help alot:)

CodePudding user response:

Just check your condition the return value from selected_option might not be a string So your condition is not getting satisfied.

selected_option = st.radio('Select page', options.values())

Try using type function to check the data type

type(selected_option)

The above type should be a string

CodePudding user response:

Here's a more robust approach.

Construct a dictionary where the keys are the radio prompts and the associated values are the functions to be called.

Like this:

import streamlit as st
from pandas import Series

class Gdrive:
    @staticmethod
    def gdrivee():
        pass
    @staticmethod
    def local():
        pass

options = {'upload from google drive': Gdrive.gdrivee, 'upload from computer': Gdrive.local}

selection = st.radio('Select page', Series(options.keys()))

options[selection]()
  • Related