How To Create A Repl Server For Snap
! Of Your Own!
Warning
As of 2024: Replit is changing their services so that you can only have 3 repls on your account. I no longer recommend using Replit as over the years the platform has gotten worse and worse and you would probably be better off hosting it somewhere else.
Yet Another Warning
As of 2023: Replit is no longer allowing users to create an "Always On" Repl using UpTimeRobot. You must have the Replit editor open for the Repl to stay on.
You can still make your own server by installing Python on your PC:
Windows:
Debian/Ubuntu-related systems:
Run sudo apt install python3
in the terminal.
When installing make sure adding Python to PATH
(Windows) is selected.
To get Flask, open Terminal/Command Prompt and type pip install flask
.
If you are using a local installation of Python to host your cloud server, create a file on your hard drive with the extension .py
and edit it with your favorite code editor. Ignore anything related to Replit and UpTimeRobot. I recommend installing Visual Studio Code (Windows) to edit your program.
When your program is running, your web server will be running on localhost, as well as on your home network (if connected to one). You must use this version of Snap! to access it: Snap! Build Your Own Blocks
The URL for your server will be localhost:8080
, or 192.168.x.x
. x
depends on what private IP address your router assigned your PC. Both URLs are printed in the console. You can also port forward and have your server accessible to all of the internet, but your firewall must be disabled. You may also purchase a domain and link it to your server.
WARNING: Flask is not meant for commercial use, and will crash if too many requests are made.
Introduction
Do you want to create your server to transfer data to Snap!? You have come to the right place! Making a Repl server to transfer data can be useful for Snap!. Using this tutorial, you can make a cloud variable server or transfer data for a game such as an account, Today you will learn about how to use Flask with Python, and connect your Python project to Snap!! Flask is a module that you can use in the text-based coding language Python.
Creating The Repl
I will be using replit.com for this project. A repl in this context is a project in Replit that is saved to the Replit cloud. Once you are logged into your Replit account, open the sidebar, and click the Button. You should then see something like this:
Make sure you have the template set to Python, name the title whatever you want, and select the owner. Then click the "+ Create Repl" Button.
Coding Flask
Once you have created the Repl, you should find yourself in the coding editor. Time to set up Flask!
First, we import flask:
from Flask import Flask
Then set a variable called "app":
app = Flask('app')
Your code should look something like this now:
from flask import Flask
app = Flask('app')
Now start the server with this code:
app.run(host='0.0.0.0', port=8080)
This will open a port and host a server with an IP. This IP Address will vary depending on where you live.
Now your code should look something like this:
from flask import Flask
app = Flask('app')
app.run(host='0.0.0.0', port=8080)
Great! Now the code to start the server has been written! Don't run the Repl yet though.
Creating Commands
Right now, the Repl is built so that can open a port and start a server. But it can't send any commands yet! So, this is how to create a URL path for our server:
@app.route('/')
This one slash represents this URL:
https://Your-Repl-Name.Your_Username.repl.co
and if using @app.route('/hello')
, it will represent THIS URL:
https://Your-Repl-Name.Your_Username.repl.co/hello
Get it? So your code should look like this right now:
from flask import Flask
app = Flask('app')
@app.route('/')
app.run(host='0.0.0.0', port=8080)
Now since the URL does not have any inputs in the URL, the code doesn't have to define anything. So use this:
def Whatever_you_want():
That Whatever_you_want
part can be anything. Just make sure not to use it again on another command. This is now this is the code:
from flask import Flask
app = Flask('app')
@app.route('/')
def Whatever_you_want():
app.run(host='0.0.0.0', port=8080)
On the next line, run whatever code you want. But for this tutorial, the code will just return a string. Whenever Snap! sends a command to the server, the server needs to return something to get a response! So to do that, just use this:
return 'Hello, World!'
That Hello, World!
part can be anything. Just make sure to add 2 spaces before the return part! Your code should look like this now:
from flask import Flask
app = Flask('app')
@app.route('/')
def Whatever_you_want():
return 'Hello, World!'
app.run(host='0.0.0.0', port=8080)
Finally! The repl is ready to be run. Click the button. It should become "☐ Stop". If you look at the console, the program is installing Flask. This will take a few moments. After that, you should see a web view appear. Congratulations! You have created a command for your web server!
Making URL Inputs For Your Server
It is time to create URL inputs for the server! Again, use @app.route('')
, but this time it will be a little different. This time use this:
@app.route('/return/<input>')
That return
and input
part can be whatever. Just make sure the input has <
and >
around it!
Your code should look like this now:
from flask import Flask
app = Flask('app')
@app.route('/')
def Whatever_you_want():
return 'Hello, World!'
@app.route('/return/<input>')
app.run(host='0.0.0.0', port=8080)
Now, the code needs to define some variables. For def Whatever_you_want():
, Code this instead:
def Whatever_you_want(input):
If there are multiple inputs, code them in and put them in order from first to last:
def Whatever_you_want(input1, input2):
The code should look like this:
from flask import Flask
app = Flask('app')
@app.route('/')
def Whatever_you_want():
return 'Hello, World!'
@app.route('/return/<input>')
def Whatever_you_want2(input):
app.run(host='0.0.0.0', port=8080)
Now it needs to return that input! Since "input" is a variable, don't use '
s. So do this instead:
@app.route('/return/<input>')
def Whatever_you_want2(input):
return input
Your code should look like this:
from flask import Flask
app = Flask('app')
@app.route('/')
def Whatever_you_want():
return 'Hello, World!'
@app.route('/return/<input>')
def Whatever_you_want2(input):
return input
app.run(host='0.0.0.0', port=8080)
Now it is time to test this. Stop the Repl, and restart it. Next, copy and paste your server URL.
You should see the URL here:
Next, open a new tab and paste the URL. Then add /return/whatever you want goes here
to the URL. It will return whatever you want goes here
! Congratulations! You know the basics of creating and coding servers!
Keep Your Server Up 24/7
Go to https://uptimerobot.com/. Log in to your account, and go to your dashboard. Press "Create New Monitor". Set the monitor type to HTTP(s). Set the friendly name to whatever you want. Set the URL to the server URL you used earlier, and set the Monitoring Interval to 5 Minutes. Leave the monitor timeout as it is. You should have something like this:
Click "Create Monitor". Congratulations! Your server will now run 24/7! Now, how will this keep your server up? UpTimeRobot will ping your Repl every five minutes, keeping it from falling asleep.
Making Snap
! Blocks To Talk To Your Server
Go to Snap! Build Your Own Blocks. You should be presented with Snap!. Time to make a block for the return input
part of your server! Go to the operators category and click the Button. Make a block like this:
Then press OK. Next, click on INPUT
and make it a text input:
Then press "Apply". Now drag a URL block into the REPORT block. Your code should look like this:
Now drag a JOIN block into the URL block. Now you need to use a CORS proxy to connect to the server. You can use any CORS Proxy. But I will use Allorigins. So, in the join block, type https://api.allorigins.win/raw?url=https://example.com/
in the first slot. Change "example.com" to the server URL you used earlier. Mine looks like this: https://api.allorigins.win/raw?url=https://Snap-Server-Tutorial.joecooldoo.repl.co
. Then in the second slot, type /return/
or whatever you used in the @app.route()
part earlier. Then click Add Input on the join block. It looks like an arrow pointing to the right. For the third slot, drag the INPUT variable into it. Your code should look something like this now:
Now press "OK". To test it, drag your new block onto the coding area. Type whatever text you want in it, and run it! It should return what you typed.
Congratulations! You have created your own Snap! Server! Thank you SO much for reading my tutorial! I can't wait to see what you guys make!