Dev DigitalOcean Hackathon: Geobot, a Geography Python Twitter Bot - Tut 01
Tags: dohackathon, python, tutorial, beginners
Published: 2021-01-02 15:07:26

[<span>Photo by <a href="https://unsplash.com/@vikceo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">vivek kumar</a> on <a href="https://unsplash.com/s/photos/whale?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>, modified(cropped)]

I started surveying the [Dev DigitalOcean Hackathon](https://dev.to/devteam/announcing-the-digitalocean-app-platform-hackathon-on-dev-2i1k) with [an article on deploying a Rust application](https://dev.to/davidedelpapa/digital-ocean-app-platform-hackathon-using-not-natively-supported-platforms-rust-rocket-55og).

However, since Rust is a much technical language, and it is not so far supported by DigitalOcean App Platform, I decided to go the Python way for this hackathon.

Grab your laptop and a beer (or some spiked eggnog: it's still Christmas season after all!).

**NOTE**: read the [official hackathon page]((https://dev.to/devteam/announcing-the-digitalocean-app-platform-hackathon-on-dev-2i1k)). If you are following this tutorial to prepare for your own hackathon, there is a link to a form to fill in order to get 50$ for 60 days, good to test the environment and to participate to the hackathon without weighing too much on the wallet. Merry Christmas and a happy new year to us!

## First Build

I will give for granted that you are using a POSIX like shell (mostly Bash, but I use ZSH). 
So the following is just routine:

```sh
mkdir geobot
cd geobot
```

In the above you can change *geobot* to give your project a different name.

Now we can create a virtual environment for our app:

```sh
python3 -m venv venv
```

Since we need to push our app to GitHub in order to trigger the DigitalOcean builds, we need a `.gitignore`.

We could create our own, but I advise to use a ready-made one, [from the official GitHub repo](https://github.com/github/gitignore):

```sh
curl -L -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
```

Now we can make our project's folder into a git repository:

```sh
git init
```

Time to activate our virtual environment:

```sh
source venv/bin/activate
```

(Remember: when you have finished, in order to deactivate the environment, just give the command `deactivate` straight to the shell).

Now we need to install our first dependencies.

We will use [Flask](https://flask.palletsprojects.com/en/1.1.x/) as API Server Framework, and [Gunicorn](https://gunicorn.org/) as WSGI HTTP server (think of it as an Apache for the Python frameworks).

```sh
pip install Flask gunicorn
```

We can also create a *requirements.txt* now, and in case of need, update it later on:

```sh
pip freeze > requirements.txt
```

Now let's create a minimalist Flask app, called *app.py*:

```python
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello world!'
```

We need to create a configuration file for Gunicorn, called *gunicorn_config.py*:

```python
bind = "0.0.0.0:8080"
workers = 2
```

That will bind your server to the port `8080` of the *localhost*, from which DigitalOcean Apps will fetch it.

## First Deploy

Now it's time to push to GitHub and deploy on DO App Platform.

Let's prepare our local repo:

```sh
git add -A
git commit -m 'First Commit'
```

Now you can create a new empty repo on GitHub, that we can enable as remote on the local one, and push our first commit to:

```sh
git remote add origin https://github.com/<yourname>/geobot.git
git push origin master
```

**NOTE**: very soon GitHub will change the defaults, and replace `master` with `main`. There will be a transparent method provided by GitHub to do so, therefore we will patiently wait the tools are in place (the official guides say to wait still). Otherwise branch now to `main` (`git branch -M main`) and push to `main` instead than to `master`.

Time for the DigitalOcean part.

Let's point our browser to the [DO App Platform Homepage](https://cloud.digitalocean.com/apps).

Click `Launch Your App`: if it's the first time it will ask you to connect your GitHub account. **Just follow the procedure**.

At the end you will be prompted to choose a repository

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8h8hdu91yudg20kzrfia.png)

Click on `Next` and in page 2 you will be prompted to pick a name and a region of deploy

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z13m8rb8dp272s65hq8w.png)

Click on `Next` and in page 3 it should have detected everything for you (that is, Python and even Gunicorn!). In case it has not, please refer to the following image for the right settings:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p5156kc6nsmhdhvsxnep.png)

Everything is OK, but the `Run Command`: click `Edit` in there and add `app:app` at the end.

It should be:

```sh
gunicorn --worker-tmp-dir /dev/shm app:app
```

Now you can save and resume the wizard.

Upon clicking `Next` we will be given the opportunity to choose our Monthly plan and the size of our virtual server (available maximum resources).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kmrcd8dzlw99d3i673ml.png)

Notice that the free plan is not available for web services, but only to host static resources.

Notice too in the bottom left of the above picture: DO defaults to the small `S` (10$ per month) server; however, there is an option for an `XS` (extra small) server (5$/Mo.); you can opt for that, at least when you are not deploying a business app, but just testing the environment.

Upon clicking on `Launch Basic App` you will be redirected to the page of your app, and you should see it loading the deploy

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/59otp42m5w37wfsfl2he.png)

If you click on `view Logs` (on the top right), you can take a look to see if the deploy got stuck somewhere.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yazfv5kqovm5w8rg8g90.png)

Once everything is done, you should see a `Deployed successfully!` message, and a link to the app under the app name:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n2dhvpe8t1g8ubomgzfn.png)

If you follow the link, you should see our Flask app greeting us from the browser window:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3n6ql0btv4ejhp6o395j.png)

Always an emotion!

## Playing with your First App

You can go ahead and change the greeting message in *app.py*, for example:

```python
@app.route('/')
def hello_world():
    return 'Hello from DO App Platform!'
```

Then, if you commit to GitHub

```sh
git add -A
git commit -m 'Showing Personalized Greeting Message'
git push origin master
```

you will see App Platform "instantly" rebuild the app for us:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3no8s17l926u4sl0vovr.png)

At the end you will see the updated message:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ifp6h2qon3rks13ufl39.png)

Notice that while it is still building, DigitalOcean will keep the old version running, and when the deploy is completed it will substitute the new version for the old.

Quite neat!

## Conclusion

For today it's all folks!
If you are completely new to this world, I hope you could follow the tutorial without problems.

Do not hesitate to write in the comments section.

As usual: stay excited, because something good's coming at the horizon!
Till next time!