Intro to Ubuntu Virtual Machines on Azure

When I search:
Node JS Server Azure, Ubuntu, JavaScript, Mongo, Postgres, Flask, VM
I turn up with all sorts of unhelpful results.
So I dedicated a couple days to creating a couple guides for common Cloud Stacks on Azure VMs to make it as simple as possible to start deploying your code to the cloud.

This is the introduction and at the bottom of this blog post you’ll see other workflows fill in.

So, Here’s a guide to deploying an Ubuntu VM on Azure:
1. Gather Materials
2. Create VM
3. Check VM using SSH

1. Gather Materials
Here’s what you’ll need:
An Azure Account
An SSH Client perhaps putty… or even Bash On Windows?

2. Create VM

Head into the Azure Portal: portal.azure.com

And Select Virtual Machines -> Then ‘Add’
selectVirtualmachines

You’ll then see a page like this:
selectubuntu

Select Ubuntu Server 14.04.

There are lots of configurable deployments available if you feel like exploring.

Then select Create, but make sure the deployment model is Resource Manager as its more future ready then the classic model:
createvm

We’ll then get to the basic configuration tab, fill out the info and pick a User name and Password that you’ll remember because you’ll need it later!

configurationbasics

If you’re not familiar with Resource Groups check out THIS ARTICLE

I’ve named my resource group: ResourceGroupOne

Hit Okay to go to the next configuration pane

Select the Size of your VM. To see all the options select ‘View All’
selectvmsize

We’re going to go with the cheapest option A1 Standard:
SelectAOne

Hit Okay to take us to our final configuration Pane, “Settings”.

settingstwo

There are a number of different settings presented here.

First up is Storage:
This will configure what we want to name the storage account for our vm. I’ve changed mine to ‘resourcegrouponestorage’, but I could have selected any of my previous storage account in the same region, in this case westus.

Second is Network:
We can configure a Virtual Network to allow our virtual machines to connect to other resource on our network by default. We can also change this later. So in this case I’m creating the default virtual network.

Again, I could have selected a previously created Virtual Network Called ‘Databases’ which is in the same region.
virtualnetworkdefault

Third is Extensions:
We won’t add any extensions

Fourth is Monitoring:
Which we’ll disable for simplicity sake, but is a very powerful tool one you start needing to make scaling decisions.

Fifth and finally is Availability:
We won’t use an availability set, until we need to scale out our app.

Here’s what the lower portion of our settings pane looks like:
settingstwoend

And we’ll select OK to finish with our settings. This will take us to the summary page so we can do a one more check on our machine, don’t get to anxious about making mistakes because we can always tear this one down and spin up another if we messed something up!
vmsummary

Hit Okay one last time!

You’ll then be taken to your dashboard where you’ll see a nice loading tile:
loadingdashboard

It’ll take ~5 minutes to spin up and then we’ll be ready to take on the world!

Once ready it’ll look like this:
clickthetile

Click the tile to hit the landing page for our VM:
vmnumberonelandingpage

See that public IP address?
We’ll use that to SSH into our machine.

In my case: 13.88.180.170 !

3. Check VM using SSH

Let’s SSH into our box.

Pull out your preferred SSH client. Here’s bash on Windows and Putty Side by Side:
sshintomachine

Notice ‘Timothy’ Triple underlined?
That’s the User Name we set during basic configuration and is paired with the password that we also set in Azure.

When you connect you might have to accept the ras2key fingerprint. It’ll look like this when using putty. Or it’ll be in the terminal using bash. Type ‘yes’ or Select Yes to continue.
sayyestowarning

Then type in your password and marvel and your creation:
typeinyourpassword

Let’s test our vm by installing updates! Yay Updates!

$ sudo apt-get update
sudoaptgetupdate

Now that you have a VM ready let’s put it to work!

Host a Node Server
Host a Python Flask Server

Pradeep Cruising on National Donut Day
Pradeep Cruising on National Donut Day

Python with Ubuntu on Windows

Now that bash is on Windows, I wanted to try and make all the other guides I’d writen for Python on Windows irrelevant.

So here’s how to setup an effective environment for Python on Ubuntu on Windows.

1. Install Bash on Windows
2. Check for updates
3. Check out the REPL
4. Install Pip
5. Install VirtualEnv
6. Install VirtualEnvWrapper
7. Create your first virtualenv
8. Configure bashrc to keep it working
9. Install some packages
10. Test Flask

1. Install Bash on Windows:
Here’s the announcement blog for context.
How to Geek has a good breakdown of making it happen.

Make sure you remember your password.

Now that its installed try opening a command prompt and typing bash.
The prompt should change like this:
bashtomnt

Notice that path?
/mnt/c/Users/TimReilly

That’s your user directory for windows where your OneDrive, Documents, Desktop, etc. exist.

You can go in there now and run python scripts that might already exist, but your probably won’t have all the necessary packages installed.

Before we move forward we want to make sure Ubuntu is up to date.

2. Check for updates
From another command prompt:
lxrun /update

And inside bash
sudo apt-get update

Thanks reddit for the tips!

3. Check out the REPL

Now run python!
$ python

Should look like this:
pythonrepl

4. Install Pip

Now we’ll install Pip:
sudo apt-get install python-pip

If you have permission issues try starting an elevated prompt:
$ sudo -i
$ apt-get install python-pip
$ exit

Use exit to return to the regular prompt.
Should look like this:
sudoi
Yay we’ve got pip!
Try pip list to see what comes standard.

5. Install VirtualEnv
Now we’re basically following along with the guide presented at hitchhikersguidetopython.com

Again, you might need to start an elevated prompt to install virtualenv.

$ sudo -i
$ pip install virtualenv
$ exit
$ cd my_project_folder
$ virtualenv venv

Then to use the VirtualEnvironment

$ source venv/bin/activate

You should now see a little (venv) before your prompt.
Like this:
virtualenvfolder

Now you’ve created a virtualenv inside of your my_project_folder directory. Which is cool, but can be confusing with git, sharing code, and testing package versions.
So we use VirtualEnvWrapper to keep our virtualenvs in the same place.

Before we move on make sure you deactivate your env
deactivate

6. Install VirtualEnvWrapper

http://virtualenvwrapper.readthedocs.io/en/latest/index.html


$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

$ export WORKON_HOME=~/
Can be customized to whichever directory you’d like to place your virtualenvs

7. Create virtualenv using virtualenvwrapper

$ mkvirtualenv venv
$ workon venv
$ deactivate

Here’s an example of what it looks like to remove our venv directory and instead use venvv which will be stored in the directory underlined in red.

venvv

8. Configure bashrc to keep it working

This might not happen to you, but when I opened a new bash terminal I had to re-source my virtualenvwrapper.sh and WORKON_HOME.

So instead I added those lines to my bashrc script.

$ sudo nano ~/.bashrc
-- type in password --

This is what it looks like in nano for me.
Ctrl+X to exit and y-enter to save.
bashrc

Then either:

Source ~/.bashrc

Or start a new command prompt->bash and try “workon” or “lsvirtualenv”

See the next image for a simple workflow.

9. Install some packages

Now lets install ‘requests’ into our newly created virtualenv:
pipinstallrequests

Isn’t that nice!

10. Test Flask
Finally we’re going to test this with flask.
First we install the required files using pip into our activated ‘venv’
Then runserver -> navigate to the designated address -> and see our site.

Here’s what it looks like:
flaskrunning

Have fun building with Python, on Ubuntu, on Windows!

Bike with pizza tied to the back
Sometimes you gotta tie a pizza to your bike.

W10 Core IoT Workshop

IoT and Windows are Better than ever with the new IoT Core for Raspberry Pi.

In this workshop we’ll have deploy Universal Windows Platform Code to a Raspberry Pi and begin communicating with the cloud and cloud services in the form of an Azure IoT Hub.

The steps include:
1. Gathering the materials
2. Preparing the Raspberry Pi
3. Installing our software
4. Connecting our LED and PushButton
5. Connecting to our raspberry pi
6. Deploying our push button app
7. Connecting our app to IoT Hub
8. Connecting our app to weather data

1. Gathering the materials

For this workshop you’ll need about $55 worth of hardware, but all the software is free.

Hardware:
1 Raspberry Pi 3 (It has built in Wifi!)
1 Micro USB to USB Cable
1 Breadboard
4 Female/Male Jumper Wires
1 Push Button Switch
1 led
1 200 ohm resistor for said led
1 Monitor with HDMI out

Software:
Windows 10 Machine version 10.0.10240 or better.
Visual Studio 2015 Update 2
The Windows 10 IoT Core Dashboard
Git

Here's my little setup
Here’s my little setup

2. Preparing Our Pi

The windows developer site has very clear instructions for setting up your device with the proper operating system so lets hop over there.

developer.microsoft.com

If you follow these instructions all the way to Step 4 of 4 you’re ready for step 5 on our workshop. So skip ahead if you followed along there.

I’m working with a raspberry pi three so my selection screen looks like this:

selection

3. Install the dashboard and Flash the OS
Next we’ll download the dashboard as prompted by the site

Then we’ll download the ISO of the raspberry pi image we want to use, then click through installer.

Once installed we can use the IoT Dashboard to flash the image onto our SD card in the form of a .ffu

Then we’ll connect the device to the network by selecting configure device.

Really though the instructions are super clear RIGHT HERE

4. Install/Update Visual Studio

We need Visual studio Update 2 to build our IoT Application, so make sure you’re up to date!

Or go here to install it: https://www.visualstudio.com/vs-2015-product-editions

Seriously though, they did an awesome job with the documentation.
If you followed along to their step 4 you’re ready for our step 5.

5. Connecting our LED and PushButton

Now that we have our pi configured and Visual Studio up to date, let’s plug in our neat hardware!

Here’s an overview of the pin layout for the raspberry pi 3:
gpiopins

And the wiring diagram for the led + push button looks like this:

wiringdiagram

Sweet, lets light it up!

6. Connecting to our raspberry pi

We need our raspberry pi’s IP address before we try to deploy our code.
Power it up with a micro USB cord and connect it to a monitor with HDMI and see the landing page for your machine.

With the Windows IOT Core Come a super helpful web portal that allows you to configure your machine through a browser.

Navigate to that IP:
http://you.have.an.ip:8080/
https://192.168.1.145:8080/ is the IP address of my machine when I tried this

You’ll be prompted with a login panel.
The default user name is:
Administrator
The default password is:
p@ssw0rd

Here’s what you should see in the portal:
coreportal

From here you can configure the name/password, get network info like the MAC address under Networking, and even test out some samples.

Sweet! This is one of my favorite Window IoT Features.

7. Deploying our push button app

The pi is setup, our computer is updated with the newest build tools, now its time to deploy some code!

Go here and clone this repo to a dev directory:
https://github.com/timmyreilly/RaspberryPiWorkshop

And setup visual studio debugging by right clicking on PushButton project like this:
properties

Then under debug set the remote machine ip to the ip you collect from the IoT Dashboard or from the raspberry pi itself by plugging in a monitor.
findip
You will then need to rebuild your solution, and restore nuget packages.

Poke around for a minute, put a break point in the buttonPin_ValueChanged method and step around to see what’s going on with the push button.

8. Connecting our app to IoT Hub

Next we’re going to turn on the IoT Hub Connection.
Go into the azure portal and create a new IoT Hub like this:
iothubone
Then create a new device called MyDevice in the IoT Hub.

More information about the IoT Hub can be found RIGHT HERE.

Then we’ll want to add our connecting strings to the app at the very bottom of MainPage.xaml.cs


static string iotHubUri = "YOURHUB.azure-devices.net";
static string deviceKey = "TOKENasQOPUesD1BmSOMETHINGCOMPLICATED";
static string deviceId = "MyDevice"; // your device name

Next we’ll uncomment this line of code on line 78:

SendDeviceToCloudMessagesAsync();

To see what’s happening in the event hub there is a small console application that we’ll run alongside to see what’s making into the cloud.

You can find that code here:
https://github.com/timmyreilly/IoTHubConsoleReader
Open that in visual studio and replace the connection string with the one provided in the Azure Portal.


string connectionString = "HostName=YOURHUBNAME.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=YOURSECRETOKENqf"

Sweet, now run both those apps at the same time, push the button, and see the glory that is Internet of things.

9. Connecting our app to weather data

Now that we’ve got it talking to the cloud, lets listen to what the clouds have to say!

We’re going to use forecast.io to provide weather data to our app, then use Speech. Synthesis to read it out over a the audio jack.

Navigate to forecast.io and signup register an account, then copy the complicated token highlighted here:

forecast

And replace what you see at line 168:

private const string FORECAST_URL = "https://api.forecast.io/forecast/YOURSECRETOKEN/";

Then if you want to be more specific about where you’re getting weather data replace the string on line 76.

var words = await GetWeatherString("37.8267,-122.423")

Make sure that the code in lines 72 through 81 are uncommented.

Deploy the app, press the button, and listen to the weather like never before!

It’s all very simple code and ready to be spun into lots of fun projects.
Here’s a list of all the ones that me and my friends could thing of:
Tap into the band app cloud
Morse code
Dance party
Send a random act of kindness
Tap into yammer
Time to finish breast feeding start and stop button
Random stats button
Gives you weather, or distance walked
Morse code each other
Send bro to someone random – twilio
Launch a middle
Listener recognition and ignore
Competitive button clicking
Obscure metadata
Best reflexes tester

Hope you had fun getting started with Windows IoT!

This workshop wouldn’t be possible without the help provided by the community. Here are some of the helpful posts that help me learn:
Windows 10 IoT Core Speech Recognition
Windows 10 IoT Core Speech Synthesis
All The ms-iot samples

Feel free to ask me questions in the comments or on twitter @timmyreilly

TOMATOES IN PROGRESS
TOMATOES IN PROGRESS

Setup a virtualenv for Python 3 on Windows

It’s essentially the same as Unix!

Especially, if you’ve followed my other guide to getting setup virtualenvs and virtualenvwrapper-win on Windows. And you’ve also installed python3!

Here’s the Script I run:

mkvirtualenv --python=C:\Python35-32\python.exe pythonthreeEnv

‘pythonthreeEnv’ is the name of my environment.

Now I can run:

workon pythonthreeEnv

Here’s a screenshot of a workflow:
pythonthreeenv

Bonus: To efficiently delete an env:

rmvirtualenv pythonthreeEnv -r

Happy Hacking!

THESE THINGS ARE AMAING. Thank you Alejandro and Argentina!
THESE THINGS ARE AMAZING. Thank you Alejandro and Argentina!

Introduction to Python on Windows with Visual Studio

Well you got just your new Windows PC.
Hopefully a crispy Surface Pro 4 (Starting at $899)

And you want to learn how to program, and Python is the language you’re going with.
Or you already know how to program and this is the first windows PC you’ve used in a while and like Python.
I don’t know you…

Well that’s fine too.

All the code and resources associated with this post can be found on GitHub

If at any point you get lost or confused, feel free to raise your hand/add a comment or PM me on twitter @timmyreilly

In this course we’ll cover:
1. Installing Python
2. Using the REPL
3. Running our first program
4. Examining the Python Folders
5. Using Pip
6. Creating, installing to, and using virtualenv and virtualenvwrapper-win
7. Examining the Envs folders!
8. setprojectdir and Workflow
9. Visual Studio
10. Virtual Envs in VS
11. Exploring Visual Studio Directories
12. Using the REPL in VS
13. Shortcuts for VS
14. Continued Learning

1. Installing Python

Windows

2. Using the REPL

The REPL stands for Read Evaluate Print Loop

Makes iterating and testing easy!

Type python into your command prompt


C:\..\> python
Python 2.7...
>>>

try these commands:

>>> x = 2 + 2
>>> print x
>>> import this
>>> type(x)
>>> dir(x)

More Practice

3. Running our first program

Use your favorite text editor. I’m using Visual Studio Code

I’ve named my example: beginnings.py

 
sentence = "Four score and seven years ago"
sentence_no_vowels = ""
for letter in sentence: 
    if letter not in "AEIOUaeiou":
        sentence_no_vowels = sentence_no_vowels + letter
         
print sentence_no_vowels

4. Examining the Python Folders

So because we’re on windows let’s take a look and explore our current folder structure just to get familiar with how Python works.

PythonDirectory

We’ll also show some helpful ways of interacting with python in our terminal

Calling Python Explicitly
> C:\Python27\python.exe
Calling Pip directly
> C:\Python27\Scripts\pip.exe list
Using Easy Install
> C:\Python27\Scripts\easy_install.exe
What Else is in there?
> dir C:\Python27\

5. Using Pip

Install a package

Python Packages are installed with pip

We’ll install requests


> pip -v
> pip install requests

Uninstall a package

pip uninstall requests

6. Creating, installing to, and using virtualenv and virtualenvwrapper-win

To another Blog Post for these steps.

7. Examining the Envs folders!

Now that we’ve used our first virtual environment where is it?
Let’s go find it! The files are in the computer!


> C:\Users\tireilly\Envs\helloworld\Scripts\pip.exe list
> C:\Users\tireilly\Envs\helloworld\Scripts\easy_install.exe

8. setprojectdir and workflow

That was fun. Let’s do it a lot.

cd into root of project


> workon newProject
> cd newProjectDirectory
> setprojectdir .
> deactivate
> workon helloworld

Now that was all fun and agnostic.
Let’s dive into Visual Studio.

9. Visual Studio

Community Edition is Free and Awesome!
– And there’s support for extensions like PTVS: http://microsoft.github.io/PTVS
– It’s a beast!
– Great for debugging, large projects and working with many different technologies.
The python offering is found in the form of Python Tools for Visual Studio.

The three things I want to introduce are Virtual Envs, the REPL, and Shortcuts

10. Virtual Envs in VS

– Visual Studio will Manage Virtual Environments for you!

11. Exploring Visual Studio Directories


> C:\visualstudio\projects\OneHourPython\OneHourPython\env\Scripts\pip.exe
> C:\visualstudio\projects\OneHourPython\OneHourPython\env\Scripts\python.exe

PythonVisualStudioDirectory

Check them out in the directory too!

12. Using the REPL in VS

13. Shortcuts for VS

Make it buttery

– Ctrl+F5 = Run Without Debugging
– Ctrl+E, Ctrl+E = Selected text to interactive
– Alt+Shift+F5 = Send file to interactive

That’s it for now, but don’t stop here the funs about to start!

Continued Learning & Other Resources:
Try Azure
Take your ideas of the Ground: BizSpark

Now that we’ve taken our first couple steps with Python, where should we go next?

See all the code associated with this project and more resources on my GitHub!

Please contact me if you have any questions: @timmyreilly

This is what happens when you get coffee on your birthday...
This is what happens when you get coffee on your birthday…

Python, Pip, virtualenv installation on Windows

No more struggles Windows Python development! I’ve found this is the best way to configure your dev environment.
This has made things much easier to get started and less of a headache overall.

We use Virtual Environment so we can test python code in encapsulated environments and to also avoid filling our base Python installation with a bunch of libraries we might use for only one project.

But Virtual Environments can be tricky if you don’t establish a good workflow. I’ll show you how to setup your python environment from Scratch and then do a very simple workflow using Flask.

SETUP
4 Steps:
Install Python
Install Pip
Install VirtualEnv
Install VirtualEnvWrapper-win

Install Python:

First Go to the Python Downloads Site.

As of March 2015 the download you want for a standard windows machine is Windows x86-64 MSI installer (The other download is for servers). Its circled here:

Download

Run the installer!
You’ll come across this page in the installer:

PythonInstaller

You’ll want to scroll down and add it to the path. If you don’t that’s okay. You can add it later.
Adding Python to the PATH will allow you to call if from the command line.

After the installation is complete double check to make sure you see python in your PATH. You can find your path by opening your control panel -> System and Security -> System -> Advanced System Settings -> Environment Variables -> Selecting Path -> Edit ->

Now you’re looking at your Path. Be Careful, if you delete or add to the path accidently you may break other programs.

You need to confirm that C:\Python27; and C:\Python27\Scripts; is part of your path.

If you do not see it in your path you can simply add it at the beginning or end of the variable value box. As you can see in the image below.

AdvancedSettings

Install Pip:

As of Python Version 2.7.9 Pip is installed automatically and will be available in your Scripts folder.

If you install a later version of Python I would recommend installing it according to this helpful stackoverflow post.

Pip is a Package manager for python which we will use to load in modules/libraries into our environments.

An example of one of these libraries is VirtualEnv which will help us keep our environments clean from other Libraries. This sounds really confusing but as you start using it you’ll begin to understand how valuable this encapsulation of modules/libraries can be.

To test that Pip is installed open a command prompt (win+r->’cmd’->Enter) and try ‘pip help’

You should see a list of available commands including install, which we’ll use for the next part:

Install virtualenv:

Now that you have pip installed and a command prompt open installing virtualenv to our root Python installation is as easy as typing ‘pip install virtualenv’
Like so:

pipinstallvirtualenv

Now we have virtualenv installed which will make it possible to create individual environments to test our code in. But managing all these environments can become cumbersome. So we’ll pip install another helpful package…

Install virtualenvwrapper-win:

This is the kit and caboodle of this guide.

Just as before we’ll use pip to install virtualenvwrapper-win. ‘pip install virtualenvwrapper-win’
Like so:

virtualenvwrapper-win

Excellent! Now we have everything we need to start building software using python! Now I’ll show you how buttery smooth it is to use these awesome tools!

USAGE
7 Steps:
Make a Virtual Environment
Connect our project with our Environment
Set Project Directory
Deactivate
Workon
Pip Install
Flask!

Make a Virtual Environemt:

Lets call it HelloWold. All we do in a command prompt is enter ‘mkvirtualenv HelloWold’
This will create a folder with python.exe, pip, and setuptools all ready to go in its own little environment. It will also activate the Virtual Environment which is indicated with the (HelloWold) on the left side of the prompt.

mkvirtualenv

Anything we install now will be specific to this project. And available to the projects we connect to this environment.

Connect our project with our Environment:

Now we want our code to use this environment to install packages and run/test code.

First lets create a directory with the same name as our virtual environment in our preferred development folder. In this case mine is ‘dev’

See here:

mkdir

HelloWold will be the root folder of our first project!

Set Project Directory:

Now to bind our virtualenv with our current working directory we simply enter ‘setprojectdir .’
Like so:

setprojectdir

Now next time we activate this environment we will automatically move into this directory!
Buttery smooth.

Deactivate:

Let say you’re content with the work you’ve contributed to this project and you want to move onto something else in the command line. Simply type ‘deactivate’ to deactivate your environment.
Like so:

deactivate

Notice how the parenthesis disappear.
You don’t have to deactivate your environment. Closing your command prompt will deactivate it for you. As long as the parenthesis are not there you will not be affecting your environment. But you will be able to impact your root python installation.

Workon:

Now you’ve got some work to do. Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move into your root project folder.

Like so:

workon

Pretty sweet! Lets get working.

Pip Install:

To use flask we need to install the packages and to do that we can use pip to install it into our HelloWold virtual environment.

Make sure (HelloWold) is to the left of your prompt and enter ‘pip install flask’
Like so:

pipinstallflask

This will bring in all the tools required to write your first web server!

Flask:

Now that you have flask installed in your virtual environment you can start coding!

Open up your favorite text editor and create a new file called hello.py and save it in your HelloWold directory.

I’ve simply taken the sample code from Flask’s website to create a very basic ‘Hello World!’ server.

I’ve named the file hello.py.

Once the code is in place I can start the server using ‘python hello.py’ this will run the python instance from your virtual environment that has flask.

See here:

webserver

You can now navigate with your browser to http://127.0.0.1:5000/ and see your new site!

Sweet. You have everything you need to start working through tutorials on Flask without worrying about gunking up your Python installations.

Let me know if you have any questions! Happy Developing!

Art Deco From Afar
Art Deco From Afar

Using Bing Maps with Flask

I’m working on a project that involves mapping and line segments.

It turns out that using the Mapping libraries of Bing and Google make this really easy.
You create ‘polylines’ out of coordinates and colors.

Check out Microsoft Maps documentation for more info and the dev page to sign up for a key.

In this example I’m using AJAX requests.

And this documentation is great for using Azure Tables
And the home directory for Python Azure Table Usage

I was struggling with pulling the data from a Database using flask and serving it to the client for an Ajax request.
The end goal is a heat map of sorts with paths being mapped and colors being changed for certain conditions.

I’m still working out the best practices for this, but its working! So take it with a grain of salt.

The code for this project can be found here: http://github.com/timmyreilly/BumpyRoads

After a couple days of tinkering, the main pieces of code that lit this up are as follows.

Get the data from the Azure Table Storage and convert it to a list

# First connect to Azure Table Service 
def connect_to_service():
    table_service = TableService(account_name=STORAGE_ACCOUNT_NAME, account_key=TABLE_STORAGE_KEY)
    print TableService
    return table_service 

# Grab the data from our table
def get_table_list(table_service=connect_to_service(), max=10, table_name='test', partitionKey='default'):
    x = table_service.query_entities(table_name)
    #print(x)
    return x 

# return it as a list  * see below for bonus on selecting color codes 
def get_json_list(entity_list=get_table_list()):
    '''
    Takes azure table list and returns json list 
    '''
    i = 0 
    response = [] 
    for r in entity_list:
        c = convert_color_key_to_rgb(int(entity_list[i].colorKey))
        t = (entity_list[i].latA, entity_list[i].longA, entity_list[i].latB, entity_list[i].longB, entity_list[i].colorKey, c[0], c[1], c[2])
        response.append(t)
        i += 1 
    # print response 
    return response 

* BONUS to manage color codes nicely I found this neat way of doing switch statements using dictionaries.

def convert_color_key_to_rgb(colorKey):
    return {
        0: [100, 100, 100], 
        1: [240, 0, 255],
        2: [0, 0, 255],
        3: [0, 255, 0],
        4: [255, 255, 0],
        5: [255, 85, 0],
        6: [255, 0, 0],
    }.get(colorKey, [100, 100, 100] )

Take that list and pass it as JSON to a template.
Still not sure if this is best practice, as on the client/javascript side it picks it up as an array object.
But this is the route we use in flask to send our data.

@app.route('/d')
def binged():   
    data = get_json_list()
    jdata = json.dumps(data)
    return render_template(
        'binged.html',
        data=jdata,
        key=token 
    )

On the template side iterate through the given array object and build and push polylines.
This was the toughest part for me to

 var map = null;
         function GetMap()
         {             
            var y = JSON.parse('{{ data|safe }}');
            // Initialize the map
            
            map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),{
                credentials:"{{ key }}",
                center: new Microsoft.Maps.Location(y[0][0],y[0][1]), 
                zoom: 10
                }); 
                       
           console.log(y);
           
           for( index = 0; index < y.length; index++ ){
               lOne = new Microsoft.Maps.Location(y[index][0], y[index][1]);
               lTwo = new Microsoft.Maps.Location(y[index][2], y[index][3]);
               //console.log(lOne);
               var lineV = new Array(lOne, lTwo);
               //console.log(y[index][4])
               var pLine = new Microsoft.Maps.Polyline(lineV, {
                   strokeColor: new Microsoft.Maps.Color(y[index][5], y[index][6], y[index][7], 100),
                   strokeThickness: 3
               });
               map.entities.push(pLine)
           }
 }

Now when I run this project locally I get something that looks like this:

Soon they won't be random.
Soon they won’t be random.

If you want to give it a shot, check out the instructions/source code on github

Feel free to reach out if you have any questions or would like to work together on a project like this.

Groovy
Groovy

Azure ML Scripting with Python

Evangelist at Microsoft
@timmyreilly
Timmyreilly.com

Free Machine Learning Platform
https://studio.azureml.net/

Full ML Documentation:
https://azure.microsoft.com/en-us/documentation/services/machine-learning/

Python ML Documentation:
https://azure.microsoft.com/en-us/documentation/articles/machine-learning-execute-python-scripts/

Gallery of Projects:
https://gallery.cortanaanalytics.com/

Gallery of Projects tagged Python:
https://gallery.cortanaanalytics.com/browse/?skip=0&orderby=trending%20desc&tags=%5B%22Python%22%5D

Breast Cancer Correlation Matrix:
https://gallery.cortanaanalytics.com/Experiment/Correlation-Matrix-2

def azureml_main(dataframe1 = None):
    import numpy as np
    import pandas as pd
    import matplotlib
    matplotlib.use("agg")  
    import matplotlib.pyplot as plt
    cm=dataframe1.corr()
    
    fig=plt.figure()
    plt.imshow(cm,interpolation='nearest')
    plt.xticks(list(range(0,len(cm.columns))),list(cm.columns.values), rotation=45)
    plt.yticks(list(range(0,len(cm.columns))),list(cm.columns.values))
    
    plt.colorbar()


    fig.savefig("CorrelationPlot.png")

    return pd.DataFrame(cm), 

Feature Importance with sklearn
https://gallery.cortanaanalytics.com/Experiment/Feature-Importance-with-sklearn-2

import numpy as np
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import pandas as pd


from sklearn.ensemble import ExtraTreesClassifier


def azureml_main(iris_data):
    X = iris_data[["sepal-length","sepal-width","petal-length","petal-width"]]
    Y = iris_data["Class"]
    
    etc = ExtraTreesClassifier(n_estimators=250, random_state=0)
    etc.fit(X,Y)
    
    feat_imp = etc.feature_importances_
    std = np.std([tree.feature_importances_ for tree in etc.estimators_],\
                 axis=0)
    indices = np.argsort(feat_imp)[::-1]
    length_fp =  len(indices)

    fig = plt.figure()
    plt.title("Feature importances")
    plt.bar(range(length_fp), feat_imp[indices],\
            color="r", yerr=std[indices], align="center")
    plt.xticks(range(length_fp), indices)
    plt.xlim([-1, length_fp])
    plt.xlabel("Feature")
    plt.ylabel("Importance")
    fig.savefig("Feature_Importance.png")
    return iris_data

Beer Forecasting Example:
https://gallery.cortanaanalytics.com/Experiment/Forecasting-Beer-Consumption-2

Using Oxford TTS Service with Python 2.7 and Requests

This is a follow up to a recent post I made about using Project Oxford with Python. If you haven’t used Project Oxford before visit that post and complete the first three steps to acquire a token.

The previous example used Python 3 and urllib which is cool. But I’m more familiar with the requests library and python 2 so I spent some time re-writing the app with requests, and in the process learned what’s actually going on.

The source code can be found here: https://github.com/timmyreilly/oxford-tts-requests.

And the example from the Project Oxford Official Repo for python 3 can be found here: https://github.com/Microsoft/ProjectOxford-ClientSDK/blob/master/Speech/TextToSpeech/Samples-Http/Python/TTSSample.py

The meat of the program is tts.py which provides the methods we use in the app to provide an access token and use it to hit the API with our words to digitize.

Here’s a link to it if you’d like to follow along: https://github.com/timmyreilly/oxford-tts-requests/blob/master/FlaskWebProject/tts.py

Then we take that token and send a POST request with our access Token and specific headers “TTS_HEADERS” and “body” which includes the text to be translated.

After we POST we receive a JSON object that we encode into base 64 and inject into our HTML Template (index.html) using views.py.

To run this project:

clone the project
create a virtual env
pip install requirements.txt
python runserver.py
visit http://localhost:5555/

This project is ready to be deployed to Azure as a Web App! If you don’t know/care about that you can simply delete all the other files besides runserver.py and the contents of FlaskWebProject

Let me know if you have any questions or would like to contribute to the next iteration!

What's underneath the Man suit?
What’s underneath the Man suit?