Run a web-scale app with the Raspberry Pi

Nočnica Mellifera - Nov 25 '19 - - Dev Community

Editing and Redeploying

My first article covered how to deploy an app to Heroku using the $35 Raspberry Pi. But deploying is not the end: It is the beginning.
image caption

After deploying the first time, you might find bugs or typos, decide to add a feature or completely revise. In which case, you will need a process to commit those changes to Heroku.

My edits

The Python test app's python code is boilerplate stuff. Nothing that's really convenient to edit for the purpose of this tutorial. So let's just alter a couple things in the HTML templates for the app, so we can immediately see our changes.

I've navigated to the directory python-getting-started/hello/templates, and double-clicked the files base.html and index.html. These files will open in the Geany editor by default.

Alt Text

On line 4 of base.html, I changed the contents of the <title></title> tags to "Edited Python app for Heroku".

Alt Text

And on line 11 of index.html, I changed the contents of the <h1></h1> tags to "It worked!"---Confidence breeds competence!

The flow

Now I will navigate to the root directory of my app in the terminal. Each time I make changes that I want to see reflected in the version hosted by Heroku, I will run the following commands in this order:

Alt Text

  1. git add . This adds your changes to the local repository's index. (Use git status to see what changes are on the index. Whatever is indexed will be recorded with the next command.)

  2. git commit This commits your changes by recording them to the local repository. This will open the editor you specified when you configured git. I am using nano to draft my commit message below.!Alt Text

  3. git push heroku master This hands the updated repository over to Heroku for deployment. (Look familiar? It's the same command we used to deploy in the previous section.) As soon as this finishes executing, you can run heroku open to see your changes.

Alt Text

Ta-dah! A Heroku app development flow is born.

Addendum: Local Hosting

To host your app locally, you need to have Postgres and some headers installed first. Run sudo apt install libpg-dev postgresql to do so. From there, the process takes just a few minutes. Let's get set up first:

Step 0: Set Up

First, as always, navigate to your app's root directory. From there, we are going to create a Python virtual environment around the getting-started directory. Run python3 -m venv getting-started to do so. For your own app, you will create a virtual environment on the directory containing your WSGI.

We should also make sure that /home/user/.local/bin (where user is your user name) is in our PATH environment variable before proceeding. You can check your PATH by executing echo $PATH. You can add the directory to your path for this session with export PATH=$PATH:/home/user/.local/bin, or for all your sessions (this will restart your Pi) with echo "PATH=$PATH:/home/user/.local/bin" >> /home/user/.profile && sudo systemctl reboot.

Step 1: Installing Requirements

Then we need to install our requirements locally. You need to run this command the first time, and whenever your requirements.txt changes: pip3 install -r requirements.txt. This will install everything listed in your requirements.txt file.

Step 2: Create Databases

This next command only need be run once: sudo -u postgres createdb python_getting_started. For your own app, you will substitute the name of your app's migration database. If your app uses any more databases, you may want to create those as well.

Step 3: Migration and Static Collection

Then we will migrate our git repository into the database we created in the previous step: python3 manage.py migrate; And run python3 manage.py collectstatic to collect the static elements of your app into a single directory for the web server. The manage.py file just lets us run django's core.management functionalities directly from the command-line. You should run these commands every time you make changes you want to see reflected locally.

Alt Text

Step 4: Host!

Finally, we run heroku local to start hosting our app locally on port 5000.

Alt Text

Open a browser to localhost:5000 and you will see your app!

Alt Text
We can see the changes made in the previous section, but this time the app is hosted on the RPi.

Conclusion

Raspberry Pi is a fine platform for web development, especially in light of the coming internet of things. With the image of rooms thick with server racks in mind, hosting and developing web applications on the Raspberry Pi reminds us that the internet really does belong to everyone. If even this little "deck of cards" can host and deploy awesome web services, then nothing is stopping anyone from making their own.

Good luck and Godspeed!

This article was co-written and would not have been possible without the Linux wizardry of J. S. Rana. If you can believe it, she's looking for work right now! Please, get in touch if you'd like a Python, Linux, or Git genius on your team.
Her email jsrana@raydialow.xyz
Her github and blog

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .