Scratchwork.xyz, Part 6: Plotting for website visitors using Dash

Prepare a new uwsgi ini, socket, python file, python virtualenv and reference in our nginx site map.

I want visitors to the website to see data from the past week. Because I don’t want them to have to know anything about the site map, I want this data to be displayed when someone lands on the root domain. We’ll name this process something logical, like “home_plotting” or something to that effect. So, first I’ll have to adjust the /etc/nginx/sites-available/$DOMAIN_NAME so that users landing on the root domain end up at the right socket/process. Easily done in a similar manner to our SQL_API subdomain, we just need something that looks like this:

location  / {
        include uwsgi_params;
        uwsgi_pass unix:/home/$USER_NAME/home_plotting/home_plotting.sock;
    }

And now check the github here under the home_plotting folder for details on the ini file. And look at post #3 for details on when we set up a similar system for the first time. Hopefully you’re used to things like the python virtual environment by now (a benefit of using it for each individual task). We will of course have to make a system process as well using systemctl, to make sure we’ve always got something running, ready to receive a request on the socket.

But uh, how do we plot something…?

Use flask + dash to create a plot

If you made a virtualenv and python file similar to that for the SQL_API, you should already have flask installed. We will be using dash to make our plot. Honestly, I recommend following along through the tutorial available on that website prior to going any further, just to familiarize yourself with dash in general.

Then, take a gander at this post at hackers and slackers. I used this as a base to get flask working with dash. Why go to this trouble? It’s probably unnecessary for my website, but what if I wanted to do some more sophisticated python stuff on the website, and still have it handled by flask? I don’t want to throw flask out yet and live in only dash world… I’m not ready for that!

If you take a look through my python script, you’ll see it’s a hacked together, chimeric version from the dash tutorial and hackers and slackers, so ugly it probably is or should be illegal in 30 states. Anyways, it works. I want to make a few points about it:

First, a lot of dash tutorials will not use a separate wsgi.py file. The main thing I cribbed from hackers and slackers was keeping that, which I like because it keeps the general file structure for this process (serving data to a visitor) the same as the one used for having an SQL API ready and listening for new data. It could in theory make things more flexible, although I don’t use that capability at all.

Second, we have to make a database connection yet again to get the data from last week. We’ve done that a few times before, but I wanted to point it out.

Third, I hate html. So I didn’t do anything fancy beyond the basic tutorial layout.