This week I’ve been making progress on the Huggable Cloud Pillow website.
The first thing to take note of is Flask.
Flask is the tiny server that allows us to host websites using Python to deliver content to the client. While on the server side you can manage complicated back ends or other processes using Python in conjunction with Flask.
This type of development is nice, because you can start seeing results right away but can take on big projects easily.
It might not be the most robust Framework, but its great for small projects…
If you want to get into Flask Web Development checkout this extensive MVA.
Small and simple, Flask is static on its own. This allows us to present static content, like templates and images easily and deals with input from the user using RESTful calls to receive input. This is great for static things with lots of user actions, but if we want something a bit more dynamic we’re going to need another tool.
In this case I’ve found Flask-SocketIO, similar to Flask-Sockets but with a few key differences highlighted by the creator (Miguel Grinberg) here.
Sockets are great for is providing live information with low latency. Basically, you can get info on the webpage without reloading or waiting for long-polling.
There are lots of ways you can extend this functionality to sharing rooms and providing communication with users and all sorts of fun stuff that is highlighted on GitHub with a great chunk of sample code. The following demo is based off of these samples.
For my project, I need the webpage to regularly check for differences in the state of the cloud and present them to the client, while also changing the image the user sees.
At first I tried to implement it using sockets passing information back and forth, but that wasn’t very stable.
The solution I’ve found, uses a background thread that is constantly running while the Flask-SocketIO Application is running, it provides a loop that I use to constantly check state of our queue.
Let’s break it down…
a. I need my website to display the current state of the cloud.
b. The Flask application can get the state by query our azure queue.
c. Once we determine a change of state we can display that information to the webpage.
d. To display the new state to the webpage we need to use a socket.
e. And pass the msg to be displayed.
This demo intends to break down problem a, c, d, and e.
I’ve created this little guide to help another developer get going quickly, with a nice piece of code available on GitHub.
The five steps to this little demo project are as follows:
1. Install Flask-SocketIO into our Virtual Environment
2. Create our background thread
3. Have it emit the current state to our client
4. Call the background thread when our page render_template’s
Celebrate! Its Friday!
Flask-SocketIO is a python package that is available for download using
So how do we call background_stuff?
We can call it wherever we want, but for this simple example we’ll put it right in our ‘/’ base route. So when we navigate to 127.0.0.1:5000 (Local Host) we’ll see the result of our background thread call.
Here’s our route:
if thread is None:
thread = Thread(target=background_stuff)
Pretty simple… Notice global thread and target=background_stuff
Creating different background threads is a good way to iterate through your changes.
I’m working with a raspberry pi and I’m learning how to collect and manage sensor data.
There is a lot of data and that’s awesome, but dealing with remote persistent storage has helped me understand some good ways of keeping stuff organized and safe.
For example: ‘Keys’ or ‘Tokens’ (Not to be confused with the Late English writer Tolkien)
These are the keys to the door of your data.
To access the contents behind these doors, you must establish that you are the key-holder. One of the ways to do this in software is to create a long complicated string that can passed to the database so you can input or output data.
As long as you keep this token secret nobody will be able to mess with your data, or use it without having to pay for it.
Now this sounds pretty simple, but what if you use that token in code that is stored in a public repository on GitHub? This is essentially like printing a bunch of keys to your house and leaving them all over town. Not safe or smart.
1. We need to keep these keys separate from the rest of your code. 2. We need to keep the file that contains those keys away from your public repository.
To do this I created a separate file called tokens.py that contains the strings of my tokens and a function called getStorageKey() that returns those strings.
Cool, we’ve satisfied our first goal.
Now we need to keep this file away from our repository. To do this we create a .gitignore file. This is a special ‘git’ file that allows us to specify which files will not be added to the remote repository.
To create a .gitignore file simply enter:
touch is a Unix command to create a file and update the access data, but not make any edits. Its the same as opening and closing without saving any changes.
After you’ve created this file you can edit it with any editor you like. I do it with sublime or VS Code because it helps me keep track of the separation occurring between visual studio and GitHub.
All you need to do is add on a separate lines the files you don’t want GitHub or just git to include.
When I first started all my .gitignore file had was:
Its expanded to include…
# the asterisk* acts as a wild card and will match any files with .pyc at the end
# anything that starts with a # is a comment and will be ignored from .gitignore
I saved this file (.gitignore) then added and committed it to the repository.
Now any files I add that end in .pyc or match tokens.py will not be added to my repository. And you will not be prompted to add them when you check git status.
What about files we’ve already have added???
If we add the rule after we added the file we want to ignore… We need to remove it from tracking on GitHub using the rm –cache command.
This will not remove it from your computer, but it will stop if from being tracked by GitHub, and will essentially be treated as if it was removed from your repository.
To pull this off simply type this command
git rm --cached filename.py
In this case I’m removing a file named ‘filename.py’.
After checking our git status we’ll see that this file is going to be removed.
Commit the changes and push to your remote repo.
You’ll see the files are still in your local directory, but are no longer in the remote or local repository. Hooray for keeping things safe!
Please let me know if you have any questions or comments.
The documentation is excellent and I recommend you spend some time reading through the resources below to round out your learning:
I ran into some troubles migrating/configuring my tables for a new app in my Django project.
I’ve been following this excellent tutorial, and ran into a bump I thought needed some clarification/update. As I’m not sure if the guide is up to date with the current version of Django.
Things I searched:
no module named models
django not creating db
models are not being created in sqlite3 Django
sqlite3 not creating table django
No migrations to apply
django sqlite3 unable to create tables
sqlite3 python package
Do these correlate with what you’re having issues with?
If so this was my solution.
First Install SQLite3 and add it too your Environment Variables -> Path
Install Page — Select: Source Code -> Zip for windows machines
I extracted it to C:/.
Now I have SQLite.exe in my root directory.
This is what the end of my Path looks like:
C:\Program Files\Microsoft\Web Platform Installer\;C:\SQLITE3.EXE\;
Sweet, now we can use SQLite in Powershell.
Configuration of Visual Studio:
Create a new app by right clicking on your Project file.
Then “Add” -> Select “Django App”
In this case my app is named book.
Sweet, now we have another Python app.
Go into your settings file and add it to the INSTALLED_APPS tuple.
Okay, now we’re configured make sure you’re SQLiteDB is properly configured as well.
Next we’ll create a model.
Following along with the tutorial.
We go into: book -> models.py and create our models.
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
Sweet. Model made. Let’s get it into our SQLite DB.
Alright now in DjangoProject1Hack (Where ‘ls’ will show db.sqlite3 among others)
We’ll be doing the migration.
Run- C:\Python27\python.exe manage.py validate
Run- C:\Python27\python.exe manage.py makemigrations
Migrations for ‘book’:
– Create model Author
– Create model Book
– Create model Publisher
– Add field publisher to book
3. Sync (This just makes sure we add what’s missing)
Run- C:\Python27\python.exe manage.py syncdb
Operations to perform:
Apply all migrations: book
Applying book.0001_initial… OK
Okay now we manage the db:
Run- C:\Python27\python.exe manage.py shell
Sample workflow in Shell:
Why should I look at this?
Ever wondered why you receive certain ads? So?
Its like a mirror into your materialist soul. What?
If you were a materialist you would be one of the types highlighted here. So?
Its interesting, and might make you smile. Fine, I’ll check it out.
At least take a look at 90210
What’s your lifestyle segmentation?
Bonus: Tesla’s Crazy new Model S.
I can’t wait to argue about this with Ryan Reilly.