posted in life, productivity ¦ Leave a comment
I thought I would start off with some tutorials on python using my raspberry pi. I should mention I am far from an expert on the Raspberry Pi, flask or even python. I am just learning and these are my notes (in tutorial form) up here for everyone to see. My hope is that that anyone correcting my python will better my own coding, and help others too.
First up we are going to start with setting up flask on a standard Raspbian install.
I am going to assume that you have a raspberry pi with the latest version of Raspbain installed. If your not make sure you have python 2.7 or better installed on your system at the very least.
Ok here we go!
We will install flask using a virtual environment. This is so that your main Python installation is not affected by any of the extensions or flask itself in the event anything goes wrong. This is only my preference to work this way. I could also add that you don’t need to have root access to get this running!
So to start you want to make a folder to house your website/app. Open up the terminal window, choose where you want to store it. My example will be in ‘/home/pi/website’
Now download the virtualenv.py and put it within your new folder ‘/home/pi/website/’
Move into that website folder if you have not already done so.
Next create the virtual environment by entering the following command:
python virtualenv.py flask
This will create a complete Python environment inside a folder called flask! Closer and closer to having our site done!
There are 2 ways to do things now. You can either activate the virtual environment and then run the next commands without the path names (which I don’t like to do). Or you can instead just invoke the interpreter you want by typing the pathname. The benefit of invoking the interpreter is that if you have more than one environment running you don’t have to keep activating and deactivating when working on more than one project.
So finally lets download and install flask
flask/bin/pip install flask
Assuming everything is ok you can just go ahead and remove the ‘virtualenv.py’ file now we won’t need it anymore.
“Hello, World” in Flask
Now you have a project folder ‘website’ and you have a sub-folder with flask and a Python interpreter. We can now get started with a Hello World. Just to prove its all working.
Within your website folder lets start making some folders.
mkdir app mkdir app/static mkdir app/templates mkdir tmp
The templates sub-folder is for templates… ok self explanatory.
Why make all this… Well later when I give more tutorials you will want to have these folders in place and ready to use.
We need to create a init script in our app folder ‘app/__init__.py’ which will create the application object (class of Flask) and then will import the views module (going to take care of that in a moment)
from flask import Flask app = Flask(__name__) from app import views
The views are handlers that respond to requests from web browsers. In Flask views are written as Python functions.
Each view function will be mapped to one or more requested URLs.
So lets write the views function ‘app/views.py’:
from app import app @app.route('/') @app.route('/index') def index(): return "Hello, World!"
There it is hello world! But we are not done yet. This view is just returning a string that will be displayed in the web browser.
The two lines @app.route(‘/’) and @app.route(‘/index’) are route decorators to create mappings from the URLs ‘/’ and ‘/index’ to the function below it.
Finally we want to get this serving to the web so we need a script to get the development web server running.
We need to make the file run.py in the website folder ‘website/run.py’
#!flask/bin/python from app import app app.run(debug = True, host="0.0.0.0")
The script will import the app variable from our app package and then invokes its run method to start the server.
Remember that the app variable has to hold the Flask instance we created before.
The ‘host=0.0.0.0′ is optional. Basically if you don’t have it flask will only accept requests from localhost or 127.0.0.1 (basically the browser on the raspberry pi itself) having the 0.0.0.0 will mean that flask will accept any request from anywhere. This is better to have if you are running the pi as a headless system and you want to test on another computer.
Just before we can run the app we need to indicate that this is an executable file:
chmod a+x run.py
Then the script can simply be executed as follows:
You will see that the server is now running and listening on port 5000. Now you can fire up your web browser and enter the following URL (changing the IP to your raspberry pi’s IP):
YEAH!!! You did it! Hello World!
Remember those route mappings from before? This means that after the port number you can enter ‘/index’ and you should also get to your hallo world page, neat huh!
Then when your done mucking around with the server you you can just hit Ctrl-C to stop it.
Whats awesome about this setup is that you can leave the server running and just simply edit any file and it will reload live to the server.. so make a change and hit refresh and you should see the change. But with one exception…. Exceptions… If you code something wrong and save it, when flask takes the new version of the file and reads it. It might crash.
Next time I want to start on some basic templating with Bootstrap and Jinja2
See you later!
posted in learning, projects, Python, raspberry pi ¦ Leave a comment
I wanted to let you in on a long term project I have been working on. At my work we have this bot running on an IRC channel that is allowing the developers to get reports on builds in progress. Also if we had any errors on our test etc. There was also a frontend website that spins around giving anyone watching realtime information on our project status. The whole system is called Rosie!
Inspired by this and jarvis from iron man movies. I set about making my own bot for my home system.
The essentals of the bot is to be able to issue commands over IRC that would either goto my XBMC raspberry pi or my other pi which I use for experiments like this and also music center.
If your using bitbucket you can request access by going here: https://bitbucket.org/theladdie/robinbot
More information coming soon!
posted in learning, projects, raspberry pi ¦ Leave a comment
I am back from the land of not blogging!!! I know it sucks you come along, sign up to the RSS feed and nothing happens. No content nadda! It is always an issue for me when work is overtaking everything I forget about the little things. Those things that are important but arnt that important if you know what I mean.
So I want to start blogging again so lets see how I get on.
Quick update on the projects:
- Project: RasPi Arcade – Scrapped due to Pimoroni’s Picade
- Project: Tasty Test System – Back burnner right now. I am not liking how the system turned out so I am waiting for inspriation to strike
I need to get back on the wagon so I hope work stress does not overwhelm me again.
posted in learning, life, projects, tasty test ¦ Leave a comment
I have been dropping a lot in terms of projects and side things in order to refocus my efforts on some of the main things and also more important things.
As part of my growing at work I am going to put a little more work in TTS in order to have it in a releasable state. Unfortunately it will be tailored for the company I work for just now. I am hoping that later I will be able to release another version that suits the general QA lackey
More news when it comes
posted in projects, QA, tasty test ¦ Leave a comment
I have neglected the blog for a while now. I never really seemed to understand the concept of baby steps. Too many projects, too many ideas and too many things to do! I could not see the Forrest for the trees.
So I am looking back and checking what a can and can’t do.
After I have cleared my list, I want to return to the blog and fix the broken links and maybe clear up what I wanna do here… Finally…
Talk again soon
posted in life, productivity, projects ¦ Leave a comment
Short post to say I am super excited about the new version of ‘Raspbian’ for the raspberry pi!!!
Raspbain if you have not already guessed is a version of the debian linux operating system specifically designed to run on the pi.
I am downloading as I type… SD Card is ready and waiting
posted in Uncategorized ¦ Leave a comment
I gotta say.. I am knackered!
I have been pulled from pillar to post trying to sort myself out with my new job, learning, projects and life. Without much luck to be honest.
2 kids do demand a lot of time (not that I am complaining) its just that I always hope for a balance. Come from work, see the kids, get something done and go to bed… Rarely goes that way and is the reason I stopped posting here.
I get bursts! Something becomes important, and then not so important, then something else is important etc. Turns out I need weeks to get something done, whatever it maybe.
So I was trying to think of a system to enable me to have the time for things. Some kind of priority system that means I get the most important things done and hopefully allow me the organized time to do other things.
I’ll report back on that later! (when I find the time HA!)
posted in life, productivity ¦ Leave a comment
A few weeks ago I finally got my raspberry pi!
If you have been living under a rock for the last year then check out their website.
In short. Credit card sized, $25 linux box that its mega awesome and run by the charity called: The Raspberry Pi Foundation.
Their concept is really interesting so check it out!
I digress… I got my Pi and I am sitting wondering what I do with it. I have been planning on installing xbmc so I can use it to stream media from the internet and my harddrive. So I am busy with that.
This morning my plans changed (at least if I manage to get another Pi this is what I will do with it)
I want to build a RasPi-Arcade!
I saw that you can get an arcade shell for the ipad where you slot the thing in and away you go. So I thought why not do something like this with the Raspberry Pi… Ok its not the most inventive idea. But for once maybe something obtainable and small enough not to anger the wife! (most important factor)
So planning the project I want to split up to the following stages (or sprints if your a scrum fan)
Stage 1 – Is the Pi a viable platform for the MAME or other emulator. If it can’t run the games then the project is a bust.
Stage 2 – Choose control system and make sure its compatible with the Raspi
Stage 3 – Convince wife to let me use the spare monitor. Design a case with controls around it.
Stage 4 – Buy the things I need to build this bad boy
Stage 5 – Build it so they come
Stage 6 – Play some arcades!!!
Stage 1 is in progress!
posted in learning, life, projects, raspberry pi ¦ Leave a comment
Check this out!!!!
Scott Hanselmans presentation about productivity was brilliant.
I watched it this morning and I think I will try out some of the system he proposes. I want to see what will work.
1st I will audit my email… Wish me luck!
posted in learning, life, productivity ¦ Leave a comment