blockpy-edu / blockpy

Blockly that's more Python than JavaScript, powered with Skulpt
Apache License 2.0
391 stars 130 forks source link

Imported libraries cannot be used and error in passing arguments #81

Closed nfkok closed 3 years ago

nfkok commented 4 years ago

Hi, I just found this interesting project. In the documentation, I saw "PythonToBlocks" and I tried out the web based GUI.

I tried to import "requests" library and use it in the python editor. My code is supposed to work and as simple as:

import requests
fetch=requests.get("http://google.com")
print (fetch)

In normal python console, I would get an output :<Response [200]>

but the using the BlockPy editor, I get an error message: IOError

Traceback:
File "answer.py", line 2
fetch=requests.get("http://google.com")
IOError: IOError: Cannot access url: URL Data was not made available for this assignment on line 2

I also tried using kwargs:

import requests
fetch=requests.get(url="http://google.com")
print (fetch)

and i get:

TypeError
Traceback:
File "answer.py", line 2
fetch=requests.get(url="http://google.com")
TypeError: TypeError: () got an unexpected keyword argument 'url' on line 2

and passing arguments into function also seems impossible:

import requests
site="http://google.com"
fetch=requests.get(url=site)
print (fetch)

which gives me:

Unused Variable
The variable site was given a value, but was never used after that.

Can I know are there solutions to these issues? Or do you have a better way or API to wrap my python code into blocks by function which can be imported easily into BlockPy?

*Issue edited after several attempts to test out BlockPy

acbart commented 3 years ago

The Requests library in Pedal does not allow you to make direct requests, because of Cross-Site Scripting issues. Instead, we rely on the instructor providing a special Url Data file to proxy requests through their static data. This is a little complex, but basically:

You click the Add New button from the files tab and choose Url Data. This creates a new file, which will be a JSON dictionary mapping filenames to their exact URLs. Here's an example from an assignment where students access weather data.

{"?weather.json": ["http://forecast.weather.gov/MapClick.php?lat=37.2327&lon=-80.4284&unit=0&lg=english&FcstType=json"]}

You then need to create a new Instructor File through the files tab. Its name should match the key of the Url Data file, but without the "?" at the start. Make sure you choose "Hidden from students, accessible programmatically". The created filename should have question mark at the start. Inside of that file, you can copy/paste whatever you want that URL to end up returning.