Skip to content

Commit

Permalink
Merge pull request ehmatthes#10 from ehmatthes/pygal_update
Browse files Browse the repository at this point in the history
Pygal update
  • Loading branch information
ehmatthes committed Mar 3, 2016
2 parents dd67445 + 44a464a commit 255e826
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 16 deletions.
9 changes: 7 additions & 2 deletions UPDATES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ Some of the libraries featured in the book have been updated recently, which is
Chapter 15
---

Pygal has been updated to version 2. All of the code in Chapter 15 will run exactly as it's written. When you get to Chapter 16, the changes in Pygal will affect some of the code. You can read about [installing Pygal](chapter_15/README.md#installing-pygal) now, or you can wait until you get to Chapter 16.
Pygal has been updated to version 2. You can install a version of Pygal that runs the code exactly as it's written in the book, or you can install the latest version of Pygal and modify what's in the book slightly. See [installing Pygal](chapter_15/README.md#installing-pygal), and see the [updates to Chapter 15](chapter_15/README.md#updates) to make tooltips appear on charts.

Chapter 16
---

Pygal's world map and internationalization features have been moved to a separate module. See the section on [installing Pygal](chapter_15/README.md#installing-pygal), and then see the specific updates for [Chapter 16](chapter_16/README.md).
Pygal's world map and internationalization features have been moved to a separate module. See the section on [installing Pygal](chapter_15/README.md#installing-pygal), and see the specific [updates for Chapter 16](chapter_16/README.md#updates).

Chapter 17
---

The [updates to Chapter 17](chapter_17/README.md#updates) allow tooltips to show on charts generated with Pygal.
45 changes: 43 additions & 2 deletions chapter_15/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Chapter 15
- [Installing matplotlib on OS X](#installing-matplotlib-on-os-x)
- [Installing matplotlib on Windows](#installing-matplotlib-on-windows)
- [Installing Pygal](#installing-pygal)
- [Updates](#updates)

Installing matplotlib
---
Expand Down Expand Up @@ -80,7 +81,7 @@ If you need help using pip, see the <a href="../chapter_12/installing_pip.md">in

Installing Pygal
---
Pygal has been updated recently, which is a good thing; you're learning a library that's being steadily improved. This also means you have two choices about how to install Pygal. You can install version 1.7 which supports the code in the book exactly as it's written, or you can install the most recent version of Pygal and modify some of the code in the book. If you install the most recent version there are some slight changes you'll need to make for the code in the second half of chapter 16, and chapter 17.
Pygal has been updated recently, which is a good thing; you're learning a library that's being steadily improved. This also means you have two choices about how to install Pygal. You can install version 1.7 which supports the code in the book exactly as it's written, or you can install the most recent version of Pygal and modify some of the code in the book. If you install the most recent version there are some slight changes you'll need to make for the code in the second half of chapters 15 and 16, and chapter 17.

### Running Pygal code exactly as it appears in the book

Expand Down Expand Up @@ -117,4 +118,44 @@ If you use the latest version, you'll need to make some slight changes to the co
- [Updates to Chapter 16 Pygal code](chapter_16/README.md)
- [Updates to Chapter 17 Pygal code](chapter_17/README.md)

[top](#)
[top](#)

Updates
---

Pygal has been updated to version 2; make sure you've read the notes about [installing Pygal](#installing-pygal) above.

If you're using Pygal version 2 or higher you'll need to add one line to each file in order to render the charts correctly. Pygal has changed the way tooltips are displayed, so if you don't add this line you won't see any tooltips when you hover over the bars on a chart.

Each time you make a chart in Pygal, add a line that tells Pygal to make an SVG file that renders correctly in a browser. For example:

hist = pygal.Bar()
hist.force_uri_protocol = 'http'

This causes Pygal to configure the SVG rendering engine to work correctly for displaying the files in a browser.

Page by page updates
---

Code that appears in bold is new, or is modified from what appears in the book.

### p. 342, die_visual.py

<pre>
hist = pygal.Bar()
<b>hist.force_uri_protocol = 'http'</b>
</pre>

### p. 343-344, dice_visual.py

<pre>
hist = pygal.Bar()
<b>hist.force_uri_protocol = 'http'</b>
</pre>

### p. 345, different_dice.py

<pre>
hist = pygal.Bar()
<b>hist.force_uri_protocol = 'http'</b>
</pre>
14 changes: 6 additions & 8 deletions chapter_15/dice_visual.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

import pygal

from die import Die

# Create a D6 and a D10.
# Create two D6 dice.
die_1 = Die()
die_2 = Die(10)
die_2 = Die()

# Make some rolls, and store results in a list.
results = []
for roll_num in range(50000):
for roll_num in range(1000):
result = die_1.roll() + die_2.roll()
results.append(result)

Expand All @@ -23,11 +22,10 @@
# Visualize the results.
hist = pygal.Bar()

hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
'13', '14', '15', '16']
hist.title = "Results of rolling two D6 dice 1000 times."
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D6 + D10', frequencies)
hist.add('D6 + D6', frequencies)
hist.render_to_file('dice_visual.svg')
32 changes: 32 additions & 0 deletions chapter_15/pygal2_update/dice_visual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pygal

from die import Die

# Create two D6 dice.
die_1 = Die()
die_2 = Die()

# Make some rolls, and store results in a list.
results = []
for roll_num in range(1000):
result = die_1.roll() + die_2.roll()
results.append(result)

# Analyze the results.
frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result+1):
frequency = results.count(value)
frequencies.append(frequency)

# Visualize the results.
hist = pygal.Bar()
hist.force_uri_protocol = 'http'

hist.title = "Results of rolling two D6 dice 1000 times."
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D6 + D6', frequencies)
hist.render_to_file('dice_visual.svg')
12 changes: 12 additions & 0 deletions chapter_15/pygal2_update/die.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from random import randint

class Die():
"""A class representing a single die."""

def __init__(self, num_sides=6):
"""Assume a six-sided die."""
self.num_sides = num_sides

def roll(self):
""""Return a random value between 1 and number of sides."""
return randint(1, self.num_sides)
30 changes: 30 additions & 0 deletions chapter_15/pygal2_update/die_visual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pygal

from die import Die

# Create a D6.
die = Die()

# Make some rolls, and store results in a list.
results = []
for roll_num in range(1000):
result = die.roll()
results.append(result)

# Analyze the results.
frequencies = []
for value in range(1, die.num_sides+1):
frequency = results.count(value)
frequencies.append(frequency)

# Visualize the results.
hist = pygal.Bar()
hist.force_uri_protocol = 'http'

hist.title = "Results of rolling one D6 1000 times."
hist.x_labels = ['1', '2', '3', '4', '5', '6']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D6', frequencies)
hist.render_to_file('die_visual.svg')
33 changes: 33 additions & 0 deletions chapter_15/pygal2_update/different_dice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from die import Die

import pygal

# Create a D6 and a D10.
die_1 = Die()
die_2 = Die(10)

# Make some rolls, and store results in a list.
results = []
for roll_num in range(50000):
result = die_1.roll() + die_2.roll()
results.append(result)

# Analyze the results.
frequencies = []
max_result = die_1.num_sides + die_2.num_sides
for value in range(2, max_result+1):
frequency = results.count(value)
frequencies.append(frequency)

# Visualize the results.
hist = pygal.Bar()
hist.force_uri_protocol = 'http'

hist.title = "Results of rolling a D6 and a D10 50,000 times."
hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
'13', '14', '15', '16']
hist.x_title = "Result"
hist.y_title = "Frequency of Result"

hist.add('D10 + D10', frequencies)
hist.render_to_file('dice_visual.svg')
18 changes: 14 additions & 4 deletions chapter_16/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You can easily [install a version of Pygal](chapter_15/README.md#installing-pyga

The first part of this update will tell you what to look for as you work through the section **Mapping Global Data Sets: JSON Format**, which begins on page 362. The second part will give you a page by page update. You can see the updated code files [here](pygal2_update/).

Overall updates
Updates
---

### The `pygal_maps_world` package
Expand Down Expand Up @@ -36,6 +36,15 @@ In the line that creates the world map, change `Worldmap` to `World`:

wm = World()

### Rendering tooltips correctly

As you probably saw in [Chapter 15](chapter_15/README.md#updates), you need to add a line each time you make a chart in order to render tooltips correctly:

<pre>
wm = World()
<b>wm.force_uri_protocol = 'http'</b>
</pre>

Page by page updates
---

Expand All @@ -49,16 +58,17 @@ Use **`from pygal.maps.world import COUNTRIES`** instead of `from pygal.i18n imp

Use **`from pygal.maps.world import World`** instead of `import pygal`.

Use **`wm = World()`** instead of `wm = pygal.Worldmap()`.
Use **`wm = World()`** instead of `wm = pygal.Worldmap()`. Then add the line **`wm.force_uri_protocol = 'http'`**.

### p. 368, *na_populations.py*

Use **`from pygal.maps.world import World`** instead of `import pygal`.

Use **`wm = World()`** instead of `wm = pygal.Worldmap()`.
Use **`wm = World()`** instead of `wm = pygal.Worldmap()`. Then add the line **`wm.force_uri_protocol = 'http'`**.

### p. 369-370, *world_populations.py*

Use **`from pygal.maps.world import World`** instead of `import pygal`.

Use **`wm = World()`** instead of `wm = pygal.Worldmap()`.
Use **`wm = World()`** instead of `wm = pygal.Worldmap()`. Then add the line **`wm.force_uri_protocol = 'http'`**.s

1 change: 1 addition & 0 deletions chapter_16/pygal2_update/americas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pygal.maps.world import World

wm = World()
wm.force_uri_protocol = 'http'
wm.title = 'North, Central, and South America'

wm.add('North America', ['ca', 'mx', 'us'])
Expand Down
1 change: 1 addition & 0 deletions chapter_16/pygal2_update/na_populations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pygal.maps.world import World

wm = World()
wm.force_uri_protocol = 'http'
wm.title = 'Populations of Countries in North America'
wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000})

Expand Down
1 change: 1 addition & 0 deletions chapter_16/pygal2_update/world_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

wm_style = RS('#336699', base_style=LCS)
wm = World(style=wm_style)
wm.force_uri_protocol = 'http'
wm.title = 'World Population in 2010, by Country'
wm.add('0-10m', cc_pops_1)
wm.add('10m-1bn', cc_pops_2)
Expand Down
29 changes: 29 additions & 0 deletions chapter_17/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Chapter 17
===

Updates
---

As you probably saw in [Chapter 15](chapter_15/README.md#updates) and [Chapter 16](chapter_16/README.md#updates), you'll need to add a line each time you make a chart in order to render tooltips correctly:

<pre>
chart = pygal.Bar()
<b>chart.force_uri_protocol = 'http'</b>
</pre>

Page by page updates
---

### p. 384-385, python_repos.py

<pre>
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
<b>chart.force_uri_protocol = 'http'</b>
</pre>

### p. 387-388, bar_descriptions.py

<pre>
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
<b>chart.force_uri_protocol = 'http'</b>
</pre>
1 change: 1 addition & 0 deletions chapter_17/pygal2_update/bar_descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

chart.title = 'Python Projects'
chart.x_labels = ['httpie', 'django', 'flask']
chart.force_uri_protocol = 'http'

plot_dicts = [
{'value': 16101, 'label': 'Description of httpie.'},
Expand Down
1 change: 1 addition & 0 deletions chapter_17/pygal2_update/python_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
my_style = LS('#333366', base_style=LCS)

my_config = pygal.Config()
my_config.force_uri_protocol = 'http'
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
Expand Down

0 comments on commit 255e826

Please sign in to comment.