-2

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
Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26
Javier
  • 3
  • 2
  • sorry for the mess, it's my first interaction here – Javier Apr 12 '23 at 18:24
  • 1
    Welcome to Stack Overflow. Please checkout the [tour], [ask] and [editing help](https://stackoverflow.com/editing-help) – Marcelo Paco Apr 12 '23 at 18:30
  • 1
    I don't think you realize that, "Prompt_toolkit is a terminal library: an ncurses or GNU Readline replacement. Web based front-ends are out af [sic] scope for this library."(Source: [Issue report: Doesn't work in Jupyter Notebook](https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1066)). (Also, be sure to click through on the links towards the bottom where you see 'Does this work with Jupyter Notebook?')This would be easily implemented in Jupyter using `input()` for the "Type the name of the fruit: " or make an interface with ipywidgets to select the fruit from among the options. – Wayne Apr 12 '23 at 18:48
  • I'm adding a separate comment to highlight what I referenced in part parenthetically in my first comments: Also see [Issue report: Does this work with Jupyter Notebook?](https://github.com/tmbo/questionary/issues/117#issuecomment-778409688) and [Issue report: Add compatibility for Jupyter Notebooks](https://github.com/tmbo/questionary/issues/118#issuecomment-779244880). Both of these are more pertinent to the 'RuntimeError: This event loop is already running.' issue featured in the title – Wayne Apr 12 '23 at 19:15
  • @Wayne thanks. I didnt know it. I wanted to implement this logic since I have a dictionary with a lot of information and I think that making that kind of recommended search while I type would be very useful for my purpose. – Javier Apr 12 '23 at 19:41

1 Answers1

1

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • 1
    Thank you for your answer, it was really helpful I thought I was going to wait some days at least for a reply, but really thanks. it's always great to know more about python and its libraries. If someone has the same problem or want to do something similar I think it would be helpful if they check this [link](https://pythonguides.com/python-tkinter-autocomplete/) too, it's about Python Tkinter Autocomplete. – Javier Apr 13 '23 at 05:10