Search code examples
pythonjupyter-notebookinteractive

Jupyter - How can I have an interactive update of my function?


def sort_category (category, file_source):
        file = file_source.loc[file_source['Odds Category'] == category]
        return file

def plot_graph (a, b):
    plt.plot(a,b)

def calcul (bankroll_init, bet_init, multiplier, odds_category, file_source):
    
    sorted_file = sort_category(odds_category, file_source)
    bankroll_current = bankroll_init
    bet_current = bet_init
    list_bankroll = []
    list_date = []

    for i in sorted_file.index:
        
        list_bankroll.append(bankroll_current)
        list_date.append(sorted_file['Date'][i])
        bankroll_current = bankroll_current - bet_current
        
        if (sorted_file['B365H'][i] == sorted_file['Min Odds'][i]) & (sorted_file['FTR'][i] == 'H'):
            bankroll_current = bankroll_current + (bet_current * sorted_file['B365H'][i])
            bet_current = bet_init

        elif (sorted_file['B365A'][i] == sorted_file['Min Odds'][i]) & (sorted_file['FTR'][i] == 'A'):
            bankroll_current = bankroll_current + (bet_current * sorted_file['B365A'][i])
            bet_current = bet_init
        else :
            bet_current = bet_current * multiplier
    
    plot_graph(list_date,list_bankroll)
   

calcul(10000,10,2,4,ligue_1)

This code plot a graphic through the "plot_graph" function, which is called in the 'calcul' function.

Is there a way to update the five arguments of my 'calcul' function with sliders or dropdown menus ? These arguments are conditions that change the graphic.

Thanks


Solution

  • I leave it to you to adapt this code for your case :

    there are lot of widgets in ipywidgets library that you can discover here. In the example I put only few of them so you can see how do we use

    import matplotlib.pyplot as plt
    import numpy as np
    import ipywidgets as wg
    
    @wg.interact(Slider_1 = wg.IntSlider(min=0, max=10, step=1, value=3, description='Sliter_A'),
                 Slider_2 = wg.FloatSlider(min=-2, max=1, step=0.1),
                 Check    = wg.Checkbox(False),
                 Radio = wg.RadioButtons(options=["string", 3.14], description="xox"),
                 DP = wg.Dropdown(options=['X', 'Y', 2, 'Random', 3],value=2, description='options :')
                 )
    def run(Slider_1, Slider_2, Check, Radio, DP):
        if(Check == True):
            plt.text(0,0.5, "see this?")
        else:
            plt.scatter(0,0, s=50, color='red')
        
        plt.text(0,0.02,Radio)
        
        x = np.linspace(-6,6,1000)
        plt.plot(x, Slider_1*x**2 + Slider_2)
        plt.xlim(-3,3)
        plt.ylim(-3,3)
    

    enter image description here