# Python function block

**URL:** <https://forum.snap.berkeley.edu/t/python-function-block/2756>\
**Category:** Snap! Feature Requests\
**Tags:** python\
**Created:** [August 8, 2020, 2:25am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756 "2020-08-08T02:25:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 8, 2020, 2:25am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/1 "2020-08-08T02:25:29Z")

</div>

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](https://cdn.forum.snap.berkeley.edu/original/2X/f/f728e13be32e7b8a310b9a875e8ee51e6199e000.png)

---

<div class="post-metadata">

**Author:** ![danielthebanana4](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/danielthebanana4/32/21092_2.png) [@danielthebanana4](https://forum.snap.berkeley.edu/u/danielthebanana4)\
**Post date:** [August 8, 2020, 2:56am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/2 "2020-08-08T02:56:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 8, 2020, 3:02am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/3 "2020-08-08T03:02:19Z")

</div>

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

`print("Hello" + "World")`

would end up being:

![Test3 script pic](https://cdn.forum.snap.berkeley.edu/original/2X/6/6d6aa785d7a4181ef567195929fd0a7aeb7a6d46.png)

,since you can't really print things.

---

<div class="post-metadata">

**Author:** ![bh](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/bh/32/31580_2.png) [@bh](https://forum.snap.berkeley.edu/u/bh)\
**Post date:** [August 8, 2020, 4:50am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/4 "2020-08-08T04:50:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cymplecy](https://forum.snap.berkeley.edu/u/cymplecy)\
**Post date:** [August 8, 2020, 9:49am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/5 "2020-08-08T09:49:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cymplecy](https://forum.snap.berkeley.edu/u/cymplecy)\
**Post date:** [August 8, 2020, 11:16am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/6 "2020-08-08T11:16:01Z")

</div>

🙂 The server is still up and running

![image](https://cdn.forum.snap.berkeley.edu/original/2X/c/c3937a7a46bcbe6230a22817af33cdbc9719dfcb.png)

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

 ![image](https://cdn.forum.snap.berkeley.edu/original/2X/f/f941a1549db3603900bf1f8597d93c9da86c2629.png)

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](https://cdn.forum.snap.berkeley.edu/original/2X/d/ddba874c5b24a5650449f24278ba8d0676465e38.png)

---

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 17, 2020, 11:17pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/7 "2020-08-17T23:17:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 17, 2020, 11:28pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/8 "2020-08-17T23:28:22Z")

</div>

It didn’t work

---

<div class="post-metadata">

**Author:** ![snapenilk](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/snapenilk/32/1947_2.png) [@snapenilk](https://forum.snap.berkeley.edu/u/snapenilk)\
**Post date:** [August 17, 2020, 11:32pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/9 "2020-08-17T23:32:04Z")

</div>

> [@blockpointstudios](#):
>
> the translate thing

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

---

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 17, 2020, 11:39pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/10 "2020-08-17T23:39:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snapenilk](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/snapenilk/32/1947_2.png) [@snapenilk](https://forum.snap.berkeley.edu/u/snapenilk)\
**Post date:** [August 17, 2020, 11:40pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/11 "2020-08-17T23:40:15Z")

</div>

Ok

---

<div class="post-metadata">

**Author:** ![cymplecy](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cymplecy](https://forum.snap.berkeley.edu/u/cymplecy)\
**Post date:** [August 18, 2020, 7:47am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/12 "2020-08-18T07:47:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![blockpointstudios](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/blockpointstudios/32/35957_2.png) [@blockpointstudios](https://forum.snap.berkeley.edu/u/blockpointstudios)\
**Post date:** [August 18, 2020, 1:04pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/13 "2020-08-18T13:04:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snapenilk](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/snapenilk/32/1947_2.png) [@snapenilk](https://forum.snap.berkeley.edu/u/snapenilk)\
**Post date:** [August 24, 2020, 11:05pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/14 "2020-08-24T23:05:52Z")

</div>

This should help: [GitHub - pypyjs/pypyjs: PyPy compiled to JavaScript](https://github.com/pypyjs/pypyjs)

---

<div class="post-metadata">

**Author:** ![roadlabs](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/roadlabs/32/2298_2.png) [@roadlabs](https://forum.snap.berkeley.edu/u/roadlabs)\
**Post date:** [October 27, 2020, 4:02am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/15 "2020-10-27T04:02:18Z")

</div>

Skulpt or Brython

---

<div class="post-metadata">

**Author:** ![snappy\_scratch](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/snappy_scratch/32/6318_2.png) [@snappy\_scratch](https://forum.snap.berkeley.edu/u/snappy_scratch)\
**Post date:** [October 27, 2020, 6:29am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/16 "2020-10-27T06:29:36Z")

</div>

No Support because python can have some _harmful_ commands. I don't want my computer's os to die because of a _CODING LANGUAGE_.

---

<div class="post-metadata">

**Author:** ![bh](https://forum.snap.berkeley.edu/user_avatar/forum.snap.berkeley.edu/bh/32/31580_2.png) [@bh](https://forum.snap.berkeley.edu/u/bh)\
**Post date:** [October 27, 2020, 6:55am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/17 "2020-10-27T06:55:55Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![18001767679](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/1/839c29/32.png) [@18001767679](https://forum.snap.berkeley.edu/u/18001767679)\
**Post date:** [October 27, 2020, 12:48pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/18 "2020-10-27T12:48:18Z")

</div>

> [@snappy\_scratch](#):
>
> harmful

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cymplecy](https://forum.snap.berkeley.edu/u/cymplecy)\
**Post date:** [October 27, 2020, 12:51pm UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/19 "2020-10-27T12:51:42Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://forum.snap.berkeley.edu/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@cymplecy](https://forum.snap.berkeley.edu/u/cymplecy)\
**Post date:** [October 30, 2020, 9:32am UTC](https://forum.snap.berkeley.edu/t/python-function-block/2756/20 "2020-10-30T09:32:58Z")

</div>

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

```auto
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

 ![image](https://cdn.forum.snap.berkeley.edu/original/2X/2/2d12520daa1dd75619297439927627d632db3975.png)

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

> **[Snap! Build Your Own Blocks](https://snap.berkeley.edu/snap/snap.html#present:Username=cymplecy&ProjectName=run%20python%20code)**

[Next page](https://forum.snap.berkeley.edu/t/python-function-block/2756.md?page=2)
