Emad
Emad

Reputation: 155

Running PySimpleGUI on Jupyter

How can PySimpleGUI be used on the web (especially Jupyter)? Should PySimpleGUIWeb be used instead? I know it can be run on replit but what I want is somewhere else.

Upvotes: 1

Views: 2383

Answers (1)

Sylphrena
Sylphrena

Reputation: 53

You can use both PySimpleGUIWeb and PySimpleGUI in Jupyter, but your decision depends on your use case.

If you are trying to run the GUI on the computer that is running the code, you can use PySimpleGUI. You'll need to be running the code on an OS with a desktop environment.

If you wish to access the GUI from any other computer, you will want to use PySimpleGUIWeb.

Example 1: Using PySimpleGUI in Jupyter

import PySimpleGUI as sg
     
sg.theme('DarkAmber')   # add a touch of color

layout = [ # all the stuff inside your window.
    [sg.Text('Some text on Row 1')],
    [sg.Text('Enter something on Row 2'), sg.InputText()],
    [sg.Button('Ok'), sg.Button('Cancel')]
]
     
# Create the Window
window = sg.Window('Window Title', layout)

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()

    # if user closes window or clicks cancel
    if event == sg.WIN_CLOSED or event == 'Cancel':
        break
    print('You entered ', values[0])
     
window.close()

Example code from: https://pysimplegui.readthedocs.io/en/latest/#jump-start

Example 2: Using PySimpleGUIWeb in Jupyter

import PySimpleGUIWeb as sg

layout = [
    [sg.Text('My Window')],
    [sg.Input(key='-IN-'), sg.Text(size=(12,1), key='-OUT-')],
    [sg.Button('Go'), sg.Button('Exit')]
]

window = sg.Window('Window Title', layout, web_port=2222, web_start_browser=False)

while True: # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
    if event == 'Go':
        window['-OUT-'].Update(values['-IN-'])

window.close()

Example code from: https://github.com/PySimpleGUI/PySimpleGUI/issues/2462

When I just did a test PySimpleGuiWeb code, I had an import error pop up using Python 3.9, so I downgraded to Python 3.8.12 and it worked great.

Navigate to http://0.0.0.0:2222/ to view the window when you run the PySimpleGuiWeb version.

You'll obviously need to install PySimpleGUIWeb and PySimpleGUI in your environment for the code to run.

Also, you may need to mess around with firewall rules on your router to access the PySimpleGUIWeb instance from other computers.

Upvotes: 2

Related Questions