Turning your application into a web app with STAR


10 May, 2020

In this world of the internet, there often comes a time that you write a script or a program that needs to be deployed as a website or a web app, doing this without the prior background in the field can be difficult and at times demotivating.

Presenting to you STAR. With STAR in a few steps you can convert you application into a website or run it from discord, yes the discord we all love.

This is just the web app
Download STAR web

This is just the discord bot
Down STAR bot

This has both the web app and the bot
DOwnload STAR complete

Once you have downloaded the tar.gz extract it into a folder

$ tar xvzf <filename>.tar.gz

Your direcory structure will look something like this

.
├── deploy.sh
├── main.py
├── .env
├── requirements.txt
├── STAR_api.py
├── STAR_bot.py
├── STAR_website
│   ├── css
│   ├── favicon.ico
│   ├── index.html
│   └── js
└──STAR_worker.py

We will get back to this

Next step is installing redis

Installing Redis

Redis is most probably in your distribution's package manager

# debian based systems (Ubuntu etc...)
$ sudo apt-get install redis
# Arch based systems (Manjaro etc...)
$ sudo pacman -S redis

If you do not find it in your distribution's package manager you can always get it from it's websitehttp://redis.io

While that is happening lets go to the next step

Installing Python dependencies

Make sure you are running python3.x

If you are running some kind of virtual environment now is the time to be in it.

$ pip install -r requirements.txt

That will install all the requirements.

Bot specific steps

If you have are planning on using the bot you need to get a api_key from discord for you bot it's pretty simple.

How to make a discord bot. This post explains step by step how to get an api key for your discord bot.

Once you have the api key put it in the.env file

Putting your code in

We are one step away from deployment

if you open up the main.py file you will find this

# main.py
def main():
return "something"

This is where you drop the code you want to run

You can put multiple functions in the file, main can call other functions or files, basically any valid python3.x function will work.

Once you put in the code lets get to the tricky part, say you are writing a monte carlo simulation to calculate the ratio of are under the inside a quarter of the circle and the square

Do not worry if you do not understand the math, this is about the parameters and the functions. Your code will look something like this.

from numpy.random import uniform
from sympy.parsing.sympy_parser
import eval_expr
import numpy

NUMPY_DICT = {a: getattr(numpy, a) for a in dir(numpy)}

def main(f, a, b, c, d, size):
"""Monte Carlo simulation"""
xs = uniform(a, b, size)
ys = uniform(c, d, size)
area = (a - b) * (c - d)
under = ys < eval_exp{"xs": xs}, NUMPY_DICT)
ratio = sum(under) / size * area
return ratio

Now we will be using type annotation on our main function's parameters. Which basically means that we will write the type of the parameters in front of the paramter like so.

def main(f: string, a: float, b: float, c: float, d: float, size: int):

and we are done, now to deployment

Deployment

Open up a terminal and go in the folder where the file was extracted and run thedeploy.sh bash script

$ ./deploy.sh

If everything goes right and you have some luck on your side you will see a stream of text flowing ignore it. In about 10 seconds that will have been deployed.

If you are running the web app it will be hosted athttp://localhost:8000/

If you downloaded the bot as well then open up discord and your server and type inhelp it will guide you through the commands on how to use it.