Python function block

Since there is a JavaScript function block, I was wondering if other types of functions could be added. I would love if you could run Python or any other language in Snap! The new functions would become a drop-down menu. Here's a picture:

untitled script pic

I’m with you! This could be a nice idea in snap!

Thanks! It would nice if a Snap developer saw this. Some commands like:

print("Hello" + "World")

would end up being:

Test3 script pic

,since you can't really print things.

Snap! is implemented in Javascript, because that's what browsers support in web pages. So we didn't have to write a Javascript interpreter to include in Snap!; it's more the other way around, with Snap! included in Javascript. So it was really easy to allow users to include a little more JS code along with Snap! itself.

But browsers don't implement Python. I think they do, still, implement Java, the language originally intended as the extension language for browsers. But everybody turns it off because of a long history of security flaws. There are a couple of other languages that aren't included in browsers but are available as browser plugins, such as Flash and Silverlight, but nobody uses them any more either, as Javascript's capabilities have improved.

(Scratch and BYOB decided to rewrite their code completely at around the same time, leading to Scratch 2.0 and Snap! 4.0. It was already clear to all of us that Javascript was the future of web browsers, but at that time there was a lot it couldn't do, such as access to the microphone and camera, and sound generation (not the same as playback of sound files, which did work). The Scratch Team decided that they couldn't have an even temporary loss of features, and so they chose Flash knowing that they'd have to have a pretty quick re-rewrite when Javascript got up to speed; Jens chose to use Javascript from the beginning and rely on it improving quickly.)

So, providing a Python interpreter and runtime system would more or less double the size of Snap!, because we'd have to have two entire languages included in our code. Not gonna happen, sorry.

Looking at this from another angle, the purpose of the JSFunction block isn't for people to write general JS programs; it's to allow one-liners that patch an existing Snap! feature or add a block that just interfaces to a JS primitive. That can only be done in the language in which Snap! is implemented.

Adding to what Brian has said, one technique to run Python is to have a program (written in Python but could be almost any language) that listens out for calls from Snap! and executes Python code that way.

I have (or had - need to find out if its still running) a test cloud webserver that would take requests from Scratch (so it would work fine with Snap!) to run Python snippets return the results back to Scratch

I'll try and see if its still up and running to show you the concept

:slight_smile: The server is still up and running

image

So, it simply emulates typing some python code into a terminal session

The idea behind it was to allow Scratch users to write python code to create their own custom reporters by diverting translate extension calls to my server

image

Could you tell me how to do that in Scratch? You know, the translate thing.

It didn’t work

Click the Add Extension button at the bottom of the list of categories. Click on the Translate extension.

I know how to get the extension. It’s just the python part that won’t work

Ok

Unfortunately it needs two things to work.

It needs a webserver that listens out for the dummy translate calls and you need to divert calls to the normal Scratch webserver using a redirection browser extension.

Hm...I tried what you said before. It’s fun making custom reporters in Scratch

This should help: https://github.com/pypyjs/pypyjs

Skulpt or Brython

No Support because python can have some harmful commands. I don't want my computer's os to die because of a CODING LANGUAGE.

There's no danger of this becoming a core part of Snap!. If anything it'd be a library that you'd have to choose to download (and probably not in our official library collection for the reason you mention).

No if the processing is done in @cymplecy's server.

That's why I don't publish the address of my server. :slight_smile:

I saw someone publish a repl.it based C/C++ server so I think that could be a reasonably secure way of doing things like this as any attacks would (well hopefully should anyway) only affect the particular instance.

I'll look into making one up

OK - I made a repl.it server to evaluate simple python code

from flask import Flask,jsonify, request
app = Flask('app')
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route('/',methods=['GET'])
def home():
  if(request.method == 'GET'): 
    return str(eval(request.args['eval'])) 

app.run(host='0.0.0.0', port=8080)

Example of use

This project is a fork of @programmer_user C/C++ one

Their's runs a whole piece of code - mine is much simpler and just uses Python eval to parse/compute a single line of code