PySimpleGUI - "forbidden" on jupyter notebook

I tried the following code but when I execute it, I get “forbidden” at the top of the jupyter notebook and the code is not being executed. What should I do?

Here is the following code

pip install PySimpleGUI

import PySimpleGUI as sg
import csv

headings = [‘Name’, ‘Address’, ‘Phone Number’, ‘City’]
header = [
sg.Text(‘Name’, pad=(0, 0), size=(15,1), justification=‘c’),
sg.Text(‘Address’, pad=(0, 0), size=(30,1), justification=‘c’),
sg.Text(‘Phone Number’, pad=(0, 0), size=(30,1), justification=‘c’),
sg.Text(‘City’, pad=(0, 0), size=(15,1), justification=‘c’)
]

layout = [header]

for row in range(0, 15):
layout.append([
sg.Input(size=(15, 1), pad=(0, 0), key=(row, 0)),
sg.Input(size=(30,1), pad=(0, 0), key=(row, 1)),
sg.Input(size=(30,1), pad=(0, 0), key=(row, 2)),
sg.Input(size=(15,1), pad=(0, 0), key=(row, 3))
])

layout.append([sg.Button(“Submit”), sg.Button(“Generate CSV”), sg.Button(“Clear”)])

window = sg.Window(‘Spreadsheet’, layout, font=‘Courier 12’)

Hmmm…I don’t see the ‘Forbidden’ using fairly up to day classic notebook or JupyterLab. Maybe you don’t have the notebook marked as ‘Trusted’ or you using a non-vanilla interface? Really though that is moot. The more important question: What is the goal here? Jupyter notebook classic/JupyterLab is its own GUI and has ways to build custom GUIs inside the notebook with Jupyter-associated tech, such as ipywidgets. Chances are you don’t need PySimpleGui to accomplish your goal if you want to do it in a Jupyter Notebook.

If you need to run PySimpleGUI, the easiest way is to separate Jupyter. Just use Python to run it like you would a Python script on your on machine. If you follow these directions you can use a modified version of your code, provided below to run this right in your browser in a temporary remote session where everything will work. It will seem like a remote linux desktop machine, and while it is running on a modified JupyterHub that won’t be obvious without some tinkering.

Minor point:

In the future, use code blocks to enter code so that it retains formatting and can be run. For example, note your for loop isn’t indented correctly in your post presently. See under the section ‘Block code formatting’ here.

UPDATE:

I found a way to run this in the browser via MyBinder not in a Jupyter notebook, see here to try it. A modified version of your code that works is found below and will be useful for running in the temporary remote session when you follow the directions:

import PySimpleGUI as sg
import csv

headings = ['Name', 'Address', 'Phone Number', 'City']
header = [
sg.Text('Name', pad=(0, 0), size=(15,1), justification='c'),
sg.Text('Address', pad=(0, 0), size=(30,1), justification='c'),
sg.Text('Phone Number', pad=(0, 0), size=(30,1), justification='c'),
sg.Text('City', pad=(0, 0), size=(15,1), justification='c')
]

layout = [header]

for row in range(0, 15):
    layout.append([
        sg.Input(size=(15, 1), pad=(0, 0), key=(row, 0)),
        sg.Input(size=(30,1), pad=(0, 0), key=(row, 1)),
        sg.Input(size=(30,1), pad=(0, 0), key=(row, 2)),
        sg.Input(size=(15,1), pad=(0, 0), key=(row, 3))
    ])

layout.append([sg.Button("Submit"), sg.Button("Generate CSV"), sg.Button("Clear")])

window = sg.Window('Spreadsheet', layout, font='Courier 12')

#based on https://www.youtube.com/watch?v=ocyZ9q0xFts
while True:
    print(window.read())
    break

window.close()
1 Like