DEV Community

loading...
Cover image for Dev DigitalOcean Hackathon: Geobot, a Geography Python Twitter Bot - Tut 01

Dev DigitalOcean Hackathon: Geobot, a Geography Python Twitter Bot - Tut 01

davidedelpapa profile image Davide Del Papa ・5 min read

[Photo by vivek kumar on Unsplash, modified(cropped)]

I started surveying the Dev DigitalOcean Hackathon with an article on deploying a Rust application.

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

mkdir geobot
cd geobot
Enter fullscreen mode Exit fullscreen mode

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

Now we can create a virtual environment for our app:

python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

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:

curl -L -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
Enter fullscreen mode Exit fullscreen mode

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

git init
Enter fullscreen mode Exit fullscreen mode

Time to activate our virtual environment:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

(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 as API Server Framework, and Gunicorn as WSGI HTTP server (think of it as an Apache for the Python frameworks).

pip install Flask gunicorn
Enter fullscreen mode Exit fullscreen mode

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

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

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

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello world!'
Enter fullscreen mode Exit fullscreen mode

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

bind = "0.0.0.0:8080"
workers = 2
Enter fullscreen mode Exit fullscreen mode

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:

git add -A
git commit -m 'First Commit'
Enter fullscreen mode Exit fullscreen mode

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:

git remote add origin https://github.com/<yourname>/geobot.git
git push origin master
Enter fullscreen mode Exit fullscreen mode

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.

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

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

Alt Text

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

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

It should be:

gunicorn --worker-tmp-dir /dev/shm app:app
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

Alt Text

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

Alt Text

Always an emotion!

Playing with your First App

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

@app.route('/')
def hello_world():
    return 'Hello from DO App Platform!'
Enter fullscreen mode Exit fullscreen mode

Then, if you commit to GitHub

git add -A
git commit -m 'Showing Personalized Greeting Message'
git push origin master
Enter fullscreen mode Exit fullscreen mode

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

Alt Text

At the end you will see the updated message:

Alt Text

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!

Discussion (0)

pic
Editor guide