Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Scratch Attach Tutorial
- MarsChompsVenus
-
Scratcher
100+ posts
Scratch Attach Tutorial
Hey scratchers! Today we're going to be learning how to use ScratchAttach! Please read the warning at the bottom before continuing.
To use ScratchAttach, you will need to be a scratcher, and it is highly recommended that you have knowledge of python before you attempt to try anything. The first thing you will need to do is set up your workspace.
I recommend using visual studios for this project, but most python programs should work. The first thing you will want to do is import SratchAttach. this is fairly simple and can be done by typing (or pasting):
pip install -U scratchattach
into your code editor's console. If you don't know where that is, ScratchAttach might not be for you.
Next is setup your Scratch project. Create a new project and load the Cloud Requests Template into it. You can find that here.
In your scratch project, drag the block that says: "Send request | request name: and wait“ onto the when flag clicked.
Great! Now share your project and continue.
For this tutorial, you need to continue by pasting the following code into the editor:
session = scratch3.login(”Username“, ”Password“) #this is necessary to use cloud variables.
conn = session.connect_cloud(”project id here“) #replace with your project id
client = scratch3.CloudRequests(conn)
notify = True
project = session.connect_project(”project id here“) #replace with your project id
@client.request
def ping(): #called when client receives request
print(”pong“)
return ”pong“ #sends back ‘pong’ to the Scratch project
@client.event
def on_ready():
print(”Request handler is running")
client.run() #make sure this is ALWAYS at the bottom of your Python file
WARNING: Although I know this code is safe to run and will not leak data, NEVER run code you find on the internet unless you trust the person giving it to you or you know it's safe.
Warnings aside, if you're still going to continue, keep reading.
Now run the program on your computer and then click the green flag on your scratch project. If the returns list on the scratch project shows pong, this should be an exhilarating moment. However if not, please post below for help.
To use ScratchAttach, you will need to be a scratcher, and it is highly recommended that you have knowledge of python before you attempt to try anything. The first thing you will need to do is set up your workspace.
I recommend using visual studios for this project, but most python programs should work. The first thing you will want to do is import SratchAttach. this is fairly simple and can be done by typing (or pasting):
pip install -U scratchattach
into your code editor's console. If you don't know where that is, ScratchAttach might not be for you.
Next is setup your Scratch project. Create a new project and load the Cloud Requests Template into it. You can find that here.
In your scratch project, drag the block that says: "Send request | request name: and wait“ onto the when flag clicked.
Great! Now share your project and continue.
For this tutorial, you need to continue by pasting the following code into the editor:
session = scratch3.login(”Username“, ”Password“) #this is necessary to use cloud variables.
conn = session.connect_cloud(”project id here“) #replace with your project id
client = scratch3.CloudRequests(conn)
notify = True
project = session.connect_project(”project id here“) #replace with your project id
@client.request
def ping(): #called when client receives request
print(”pong“)
return ”pong“ #sends back ‘pong’ to the Scratch project
@client.event
def on_ready():
print(”Request handler is running")
client.run() #make sure this is ALWAYS at the bottom of your Python file
WARNING: Although I know this code is safe to run and will not leak data, NEVER run code you find on the internet unless you trust the person giving it to you or you know it's safe.
Warnings aside, if you're still going to continue, keep reading.
Now run the program on your computer and then click the green flag on your scratch project. If the returns list on the scratch project shows pong, this should be an exhilarating moment. However if not, please post below for help.
Last edited by MarsChompsVenus (July 28, 2024 00:16:02)
- JamesTheScratcherBoy
-
Scratcher
89 posts
Scratch Attach Tutorial
Ummm, Idk how to handle a request with 2 arguments
- MarsChompsVenus
-
Scratcher
100+ posts
Scratch Attach Tutorial
Ummm, Idk how to handle a request with 2 arguments
I don't know what you mean! Do you mind elaborating?
- Discussion Forums
- » Advanced Topics
-
» Scratch Attach Tutorial