Search code examples
pythonjupyter-notebookruntime-error

RuntimeError: This event loop is already running. This happens when I run my code in jupyter


I'm trying to implement this logic in my code but there's always an error the RuntimeError: This event loop is already running" This happens when I run my code in jupyter, but its ok in Visual Studio Code. Any idea on how to solve it?

from prompt_toolkit import prompt

from prompt_toolkit.completion import WordCompleter

#Dictionary of fruits and prices:

fruits = {
    "apple": 2.5,
    "orange": 3.0,
    "pinapple": 5.0,
    "banana": 1.5,
    "kiwi": 4.0,
    "mango": 6.0
}

#Complete fruits:

fruits_completer = WordCompleter(list(fruits.keys()))

#Ask user for the name of the fruit:

fruits= prompt("Type the name of the fruit: ", completer=fruits_completer)

#Show the price of the choosen fruit:

price = fruits.get(fruit)

if price:

    print(f"The price of the {fruit} is: {price}")

else:

    print("The fruit is not in the list")

This should happen When I run it tells me to type the name of the fruit then if I type app, it show me apple as an option, then if I choose it (and press enter) it should show me the price of that fruit

Type the name of the fruit: app

Type the name of the fruit: apple

The price is: 2.5

Solution

  • Based on here and your code, an option:

    import ipywidgets as widgets
    from ipywidgets import interactive
    
    fruits = { "apple": 2.5, "orange": 3.0, "pinapple": 5.0, "banana": 1.5, "kiwi": 4.0, "mango": 6.0 }
    
    drop1 = widgets.Dropdown(options=fruits.keys(), value='apple', description='Fruit:', disabled=False)
    
    def update_dropdown(fruit):
        info = (f"The price of the {fruit} is: {fruits[fruit]}")
        print(info)  
            
    w = interactive(update_dropdown, fruit=drop1) 
    display(w)
    

    For a place to try this code without first installing ipywidgets on your machine, go here and click on 'launch binder'. Paste in the code block above when the session starts and the notebook comes up. Then run the code and change the drop-down to choose different fruit.


    Add autocomplete

    There's a simple autocomplete example that works here. You just put autoFill.py from there in the same directory with your notebook. (This will work in the same sessions I referenced above if you add autoFill.py.) Then try:

    import autoFill as af
    from ipywidgets import HTML, HBox
    
    def open(value):
        show.value=f"The price of the {value.new} is: {fruits[value.new]}"
        
    options = ['Football', 'Basketball', 'Voleyball', 'Basketcase', 'Showcase', 'Footer', 'Header', 'Viewer', 'Footage', 'Showtime', 'Show Business']
    fruits = { "apple": 2.5, "orange": 3.0, "pinapple": 5.0, "banana": 1.5, "kiwi": 4.0, "mango": 6.0 }
    autofill = af.autoFill(list(fruits.keys()),callback=open)
    
    show = HTML('Result will be displayed here!')
    
    display(HBox([autofill,show]))
    

    As you start typing app, it will opt you to choose apple or pinapple[sic].

    Alternatively, here suggests Bokeh has an autocompleteInput widget. That code didn't work for me, but a variation on the current autocompleteinput example here worked:

    from bokeh.io import show
    from bokeh.io import output_notebook
    from bokeh.models import AutocompleteInput
    from bokeh.sampledata.world_cities import data
    output_notebook()
    completion_list = data["name"].tolist()
    
    auto_complete_input =  AutocompleteInput(title="Enter a city:", completions=completion_list)
    
    show(auto_complete_input)
    

    After I ran the following in the cell above it:

    %pip install bokeh
    import bokeh
    bokeh.sampledata.download()
    

    Although, the fact there was choices coming up below for the Bokeh widget wasn't really obvious unless I scrolled looking for them as the space didn't get well generated below. Both in classic notebook and JupyterLab.

    And obviously that would need to be adapted. I just used the example code and even getting that to work wasn't as straightforward as expected.