100% found this document useful (2 votes)
26 views

Web App Development and Real-Time Web Analytics with Python: Develop and Integrate Machine Learning Algorithms into Web Apps Nokeri instant download

The document is about 'Web App Development and Real-Time Web Analytics with Python' by Tshepo Chris Nokeri, focusing on integrating machine learning algorithms into web applications. It includes a comprehensive table of contents detailing various chapters on data tabulation, charting, HTML essentials, and Python web frameworks. The book aims to guide readers in developing secure and scalable web applications using Python and related technologies.

Uploaded by

kdboymage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
26 views

Web App Development and Real-Time Web Analytics with Python: Develop and Integrate Machine Learning Algorithms into Web Apps Nokeri instant download

The document is about 'Web App Development and Real-Time Web Analytics with Python' by Tshepo Chris Nokeri, focusing on integrating machine learning algorithms into web applications. It includes a comprehensive table of contents detailing various chapters on data tabulation, charting, HTML essentials, and Python web frameworks. The book aims to guide readers in developing secure and scalable web applications using Python and related technologies.

Uploaded by

kdboymage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Web App Development and Real-Time Web Analytics

with Python: Develop and Integrate Machine


Learning Algorithms into Web Apps Nokeri install
download
https://ebookmeta.com/product/web-app-development-and-real-time-
web-analytics-with-python-develop-and-integrate-machine-learning-
algorithms-into-web-apps-nokeri/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Real-Time Twilio and Flybase. Build Real-Time Web Apps


Using Twilio and Flybase with Node.js 1st Edition Roger
Stringer

https://ebookmeta.com/product/real-time-twilio-and-flybase-build-
real-time-web-apps-using-twilio-and-flybase-with-node-js-1st-
edition-roger-stringer/

Web Development with Clojure Build Bulletproof Web Apps


with Less Code 3rd Edition Dmitri Sotnikov Scot Brown

https://ebookmeta.com/product/web-development-with-clojure-build-
bulletproof-web-apps-with-less-code-3rd-edition-dmitri-sotnikov-
scot-brown/

Web Application Development with Streamlit: Develop and


Deploy Secure and Scalable Web Applications to the
Cloud Using a Pure Python Framework 1st Edition
Mohammad Khorasani
https://ebookmeta.com/product/web-application-development-with-
streamlit-develop-and-deploy-secure-and-scalable-web-
applications-to-the-cloud-using-a-pure-python-framework-1st-
edition-mohammad-khorasani/

Leadership Team Coaching in Practice Case Studies on


Developing High Performing Teams 2nd Edition Peter
Hawkins

https://ebookmeta.com/product/leadership-team-coaching-in-
practice-case-studies-on-developing-high-performing-teams-2nd-
edition-peter-hawkins/
Codename Dweeb Friends to Lovers Christmas Military
Romance 1st Edition Mazzy King

https://ebookmeta.com/product/codename-dweeb-friends-to-lovers-
christmas-military-romance-1st-edition-mazzy-king/

Machinability and Tribological Performance of Advanced


Alloys 2nd Edition George Pantazopoulos

https://ebookmeta.com/product/machinability-and-tribological-
performance-of-advanced-alloys-2nd-edition-george-pantazopoulos/

Budgeting For Dummies Athena Valentine Lent

https://ebookmeta.com/product/budgeting-for-dummies-athena-
valentine-lent/

Vultures First Edition Dalpat Chauhan

https://ebookmeta.com/product/vultures-first-edition-dalpat-
chauhan/

ESP32 Formats and Communication: Application of


Communication Protocols with ESP32 Microcontroller 1st
Edition Neil Cameron

https://ebookmeta.com/product/esp32-formats-and-communication-
application-of-communication-protocols-with-
esp32-microcontroller-1st-edition-neil-cameron/
Toddlers Parents and Culture 1st Edition Maria A.
Gartstein

https://ebookmeta.com/product/toddlers-parents-and-culture-1st-
edition-maria-a-gartstein/
Web App Development
and Real-Time Web
Analytics with Python
Develop and Integrate Machine Learning
Algorithms into Web Apps

Tshepo Chris Nokeri
Web App Development
and Real-Time Web
Analytics with Python
Develop and Integrate Machine
Learning Algorithms into Web Apps

Tshepo Chris Nokeri


Web App Development and Real-Time Web Analytics with Python: Develop and
Integrate Machine Learning Algorithms into Web Apps

Tshepo Chris Nokeri


Pretoria, South Africa

ISBN-13 (pbk): 978-1-4842-7782-9 ISBN-13 (electronic): 978-1-4842-7783-6


https://doi.org/10.1007/978-1-4842-7783-6

Copyright © 2022 by Tshepo Chris Nokeri


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: James Markham
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image by Andrew Kliatskyi on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.
springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science
+ Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484277829. For more
detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
I would like dedicate this book to my family, friends, and
anyone who played a pivotal role in any aspect of my life,
including the Apress team for the continous support.
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi

About the Technical Reviewer������������������������������������������������������������������������������� xiii


Acknowledgments���������������������������������������������������������������������������������������������������xv

Chapter 1: Tabulating Data and Constructing Static 2D and 3D Charts�������������������� 1


Tabulating the Data����������������������������������������������������������������������������������������������������������������������� 1
2D Charting����������������������������������������������������������������������������������������������������������������������������������� 4
Box-Whisker Plot��������������������������������������������������������������������������������������������������������������������� 6
Histogram�������������������������������������������������������������������������������������������������������������������������������� 7
Line Plot����������������������������������������������������������������������������������������������������������������������������������� 8
Scatter Plot������������������������������������������������������������������������������������������������������������������������������ 9
Density Plot��������������������������������������������������������������������������������������������������������������������������� 10
Violin Plot������������������������������������������������������������������������������������������������������������������������������� 11
Regression Plot��������������������������������������������������������������������������������������������������������������������� 13
Joint Plot������������������������������������������������������������������������������������������������������������������������������� 13
Heatmap�������������������������������������������������������������������������������������������������������������������������������� 14
3D Charting��������������������������������������������������������������������������������������������������������������������������������� 17
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 19

Chapter 2: Interactive Tabulation and Charting������������������������������������������������������ 21


Plotly������������������������������������������������������������������������������������������������������������������������������������������� 21
Tabulating the Data with Plotly��������������������������������������������������������������������������������������������������� 22
Interactive Charting��������������������������������������������������������������������������������������������������������������������� 23
2D Charting��������������������������������������������������������������������������������������������������������������������������������� 23
Box Plot��������������������������������������������������������������������������������������������������������������������������������� 26
Violin Plot������������������������������������������������������������������������������������������������������������������������������� 27
Histogram������������������������������������������������������������������������������������������������������������������������������ 28

v
Table of Contents

Scatter Plot���������������������������������������������������������������������������������������������������������������������������� 32
Density Plot��������������������������������������������������������������������������������������������������������������������������� 34
Bar Chart������������������������������������������������������������������������������������������������������������������������������� 36
Pie Chart�������������������������������������������������������������������������������������������������������������������������������� 38
Sunburst�������������������������������������������������������������������������������������������������������������������������������� 38
Choropleth Map��������������������������������������������������������������������������������������������������������������������������� 41
Heatmap�������������������������������������������������������������������������������������������������������������������������������� 42
3D Charting��������������������������������������������������������������������������������������������������������������������������������� 43
Indicators������������������������������������������������������������������������������������������������������������������������������������ 44
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 45

Chapter 3: Containing Functionality and Styling for Interactive Charts����������������� 47


Updating Graph Layout���������������������������������������������������������������������������������������������������������������� 47
Updating Plotly Axes�������������������������������������������������������������������������������������������������������������� 48
Including Range Slider���������������������������������������������������������������������������������������������������������� 48
Including Buttons to a Graph������������������������������������������������������������������������������������������������� 49
Subplots�������������������������������������������������������������������������������������������������������������������������������������� 51
Styling Charts������������������������������������������������������������������������������������������������������������������������������ 56
Altering Color Schemes��������������������������������������������������������������������������������������������������������� 57
Color Sequencing������������������������������������������������������������������������������������������������������������������ 57
Customizing Traces���������������������������������������������������������������������������������������������������������������� 59
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 61

Chapter 4: Essentials of HTML�������������������������������������������������������������������������������� 63


Communication Between a Web Browser and a Web Server������������������������������������������������������ 63
URL Structure������������������������������������������������������������������������������������������������������������������������������ 63
Domain Hosting��������������������������������������������������������������������������������������������������������������������������� 64
Shared Web Hosting�������������������������������������������������������������������������������������������������������������� 64
Managed Web Hosting����������������������������������������������������������������������������������������������������������� 65
Web Servers�������������������������������������������������������������������������������������������������������������������������� 66
HyperText Markup Language������������������������������������������������������������������������������������������������������ 66
HTML Elements��������������������������������������������������������������������������������������������������������������������� 67

vi
Table of Contents

Meta Tag������������������������������������������������������������������������������������������������������������������������������������� 75
Practical Example����������������������������������������������������������������������������������������������������������������������� 75
Viewing Web Page Source���������������������������������������������������������������������������������������������������������� 78
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 78

Chapter 5: Python Web Frameworks and Apps������������������������������������������������������ 79


Web Frameworks������������������������������������������������������������������������������������������������������������������������ 79
Web Apps������������������������������������������������������������������������������������������������������������������������������������ 80
Flask������������������������������������������������������������������������������������������������������������������������������������������� 80
WSGI�������������������������������������������������������������������������������������������������������������������������������������� 80
Werkzeug������������������������������������������������������������������������������������������������������������������������������� 80
Jinja��������������������������������������������������������������������������������������������������������������������������������������� 81
Installing Flask���������������������������������������������������������������������������������������������������������������������� 81
Initializing a Flask Web App��������������������������������������������������������������������������������������������������� 81
Flask App Code���������������������������������������������������������������������������������������������������������������������� 82
Deploy a Flask Web App�������������������������������������������������������������������������������������������������������� 82
Dash�������������������������������������������������������������������������������������������������������������������������������������������� 82
Installing Dash Dependencies����������������������������������������������������������������������������������������������� 83
Initializing a Dash Web App��������������������������������������������������������������������������������������������������� 83
Dash Web App Code��������������������������������������������������������������������������������������������������������������� 83
Deploy a Dash Web App��������������������������������������������������������������������������������������������������������� 84
Jupyter Dash������������������������������������������������������������������������������������������������������������������������������� 84
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 85

Chapter 6: Dash Bootstrap Components����������������������������������������������������������������� 87


Number Input������������������������������������������������������������������������������������������������������������������������������ 88
Text Area������������������������������������������������������������������������������������������������������������������������������������� 89
Select������������������������������������������������������������������������������������������������������������������������������������������ 89
Radio Items��������������������������������������������������������������������������������������������������������������������������������� 90
Checklist������������������������������������������������������������������������������������������������������������������������������������� 91
Switches������������������������������������������������������������������������������������������������������������������������������������� 92
Tabs��������������������������������������������������������������������������������������������������������������������������������������������� 93

vii
Table of Contents

Button����������������������������������������������������������������������������������������������������������������������������������������� 94
Table������������������������������������������������������������������������������������������������������������������������������������������� 95
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 97

Chapter 7: Styling and Theming a Web App����������������������������������������������������������� 99


Styling����������������������������������������������������������������������������������������������������������������������������������������� 99
Cascade Styling Sheet�������������������������������������������������������������������������������������������������������������� 100
Bootstrap���������������������������������������������������������������������������������������������������������������������������������� 102
Dash Bootstrapping������������������������������������������������������������������������������������������������������������������� 103
Dash Core Components������������������������������������������������������������������������������������������������������� 104
Dash Bootstrap Components����������������������������������������������������������������������������������������������� 104
Implementing Dash Bootstrap Components Theming��������������������������������������������������������� 104
Dash HTML Components����������������������������������������������������������������������������������������������������� 106
Dash Web Application Layout Design���������������������������������������������������������������������������������� 106
Responsive Grid System����������������������������������������������������������������������������������������������������������� 107
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 109

Chapter 8: Building a Real-Time Web App������������������������������������������������������������ 111


Dash App Structure������������������������������������������������������������������������������������������������������������������� 112
Importing Key Dependencies���������������������������������������������������������������������������������������������������� 112
Loading an External CSS File����������������������������������������������������������������������������������������������� 115
Loading the Bootstrap Icons Library����������������������������������������������������������������������������������������� 116
Initializing a Web App���������������������������������������������������������������������������������������������������������� 116
Navigation Bars������������������������������������������������������������������������������������������������������������������������� 116
Top Navigation Bar�������������������������������������������������������������������������������������������������������������� 117
Specifying the Responsive Side Navigation Bar������������������������������������������������������������������ 121
Specifying the Web App CSS Code�������������������������������������������������������������������������������������������� 123
Side Navigation Bar Menus and Submenus������������������������������������������������������������������������������ 124
Search Functionality����������������������������������������������������������������������������������������������������������������� 127
Creating Interactive Charts�������������������������������������������������������������������������������������������������� 129
Containing an Interactive Table and Allowing Generating a Report and
Enabling Download�������������������������������������������������������������������������������������������������������������� 131

viii
Table of Contents

Specifying the App Layout�������������������������������������������������������������������������������������������������������� 134


Specifying a Callback Function������������������������������������������������������������������������������������������������� 135
Callback for a Responsive Side Navigation Bar������������������������������������������������������������������� 136
Callback for URL Routing����������������������������������������������������������������������������������������������������� 137
Specifying a Callback Function for Unhiding Content��������������������������������������������������������� 138
Specifying a Callback Function for Interactive Charts��������������������������������������������������������� 139
Specifying a Callback Function for Unhiding an Interactive Table��������������������������������������� 141
Specifying a Callback Function for an Interactive Table������������������������������������������������������ 142
Specifying a Callback Function for Callback for Data Download����������������������������������������� 143
Run the Dash App���������������������������������������������������������������������������������������������������������������������� 143
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 144

Chapter 9: Basic Web App Authentication������������������������������������������������������������ 145


Authentication with Dash Auth�������������������������������������������������������������������������������������������������� 145
Authentication with Flask��������������������������������������������������������������������������������������������������������� 148
Login Form�������������������������������������������������������������������������������������������������������������������������������� 150
Login on Home Page����������������������������������������������������������������������������������������������������������������� 155
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 158

Chapter 10: Dash into a Full Website�������������������������������������������������������������������� 159


Home Page�������������������������������������������������������������������������������������������������������������������������������� 159
Footer Navigation Bar���������������������������������������������������������������������������������������������������������� 168
Banner��������������������������������������������������������������������������������������������������������������������������������� 173
Callback to Collapse the Navigation for Small Screens������������������������������������������������������� 175
Home Page�������������������������������������������������������������������������������������������������������������������������������� 176
Contact Us��������������������������������������������������������������������������������������������������������������������������������� 176
Billing/Checkout������������������������������������������������������������������������������������������������������������������������ 183
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 188

Chapter 11: Integrating a Machine Learning Algorithm into a Web App�������������� 189
An Introduction to Linear Regression���������������������������������������������������������������������������������������� 189
An Introduction to sklearn��������������������������������������������������������������������������������������������������������� 190
Preprocessing��������������������������������������������������������������������������������������������������������������������������� 191

ix
Table of Contents

Splitting Data into Training and Test Data���������������������������������������������������������������������������� 192


Standardization������������������������������������������������������������������������������������������������������������������� 192
Training an Algorithm���������������������������������������������������������������������������������������������������������������� 192
Predictions��������������������������������������������������������������������������������������������������������������������������� 193
Integrating an Algorithm to a Web App������������������������������������������������������������������������������������� 194
Initializing a Web App���������������������������������������������������������������������������������������������������������� 195
Navigation Bars������������������������������������������������������������������������������������������������������������������������� 195
Search Functionality������������������������������������������������������������������������������������������������������������ 200
Containing Interactive Tables for Results���������������������������������������������������������������������������� 201
Specifying the App Layout and Callbacks for Responsive Side Menus and
URL Routing������������������������������������������������������������������������������������������������������������������������� 203
Specifying a Callback to Load Independent Variables Values���������������������������������������������� 206
Specifying a Callback for Loading the Dependent Variable Values�������������������������������������� 206
Specifying a Callback for Descriptive Statistics����������������������������������������������������������������������� 207
Specifying a Callback for Correlation Analysis Results������������������������������������������������������� 208
Specifying a Callback for an Algorithm’s Predictions���������������������������������������������������������� 209
Specifying a Callback for an Algorithm’s Intercept and Coefficients����������������������������������� 210
Specifying a Callback for an Algorithm’s Evaluation Results���������������������������������������������� 211
Running the Dash App��������������������������������������������������������������������������������������������������������� 213
Conclusions������������������������������������������������������������������������������������������������������������������������������� 213

Chapter 12: Deploying a Web App on the Cloud��������������������������������������������������� 215


Integrated Development Environment�������������������������������������������������������������������������������������� 215
PyCharm������������������������������������������������������������������������������������������������������������������������������ 215
Virtual Environment������������������������������������������������������������������������������������������������������������������� 216
File Structure���������������������������������������������������������������������������������������������������������������������������� 218
Integrating Innumerable Python Files��������������������������������������������������������������������������������������� 219
Hosting Web Apps��������������������������������������������������������������������������������������������������������������������� 219
Dash Enterprise������������������������������������������������������������������������������������������������������������������� 219
Heroku��������������������������������������������������������������������������������������������������������������������������������� 219
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 221

Index��������������������������������������������������������������������������������������������������������������������� 223
x
About the Author
Tshepo Chris Nokeri harnesses advanced analytics and
artificial intelligence to foster innovation and optimize
business performance. He delivers complex solutions to
companies in the mining, petroleum, and manufacturing
industries. He received a bachelor’s degree in information
management. He graduated with honours in business
science from the University of the Witwatersrand,
Johannesburg, on a Tata Prestigious Scholarship and a
Wits Postgraduate Merit Award. He was unanimously awarded the Oxford University
Press Prize. Tshepo has authored three books: Data Science Revealed (Apress, 2021),
Implementing Machine Learning in Finance (Apress, 2021), and Econometrics and Data
Science (Apress, 2022).

xi
About the Technical Reviewer
Brij Kishore Pandey works as a software engineer, architect,
and strategist at ADP. He has a wide interest in software
development using cutting-edge tools/technologies in
cloud computing, data engineering, data science, artificial
intelligence, and machine learning. He has 12 years of
experience working with global corporate leaders, including
JP Morgan Chase, American Express, 3M Company, Alaska
Airlines, Cigna Healthcare, and ADP.

xiii
Acknowledgments
Writing a single-authored book is demanding, but I received firm support and active
encouragement from my family and dear friends. Many heartfelt thanks to the Apress
team for their backing throughout the writing and editing process. And my humble
thanks to all of you for reading this; I earnestly hope you find it helpful.

xv
CHAPTER 1

Tabulating Data and


Constructing Static 2D
and 3D Charts
This chapter introduces the basics of tabulating data and constructing static graphical
representations. It begins by demonstrating an approach to extract and tabulate data
by implementing the pandas and SQLAlchemy libraries. Subsequently, it reveals two
prevalent 2D and 3D charting libraries: Matplotlib and seaborn. It then describes a
technique for constructing basic charts (i.e., box-whisker plot, histogram, line plot,
scatter plot, density plot, violin plot, regression plot, joint plot, and heatmap).

T abulating the Data


The most prevalent Python library for tabulating data comprising rows and columns
is pandas. Ensure that you install pandas in your environment. To install pandas in a
Python environment, use pip install pandas. Likewise, in a conda environment, use
conda install -c anaconda pandas.
The book uses Python version 3.7.6 and pandas version 1.2.4. Note that examples in
this book also apply to the latest versions.
Listing 1-1 extracts data from a CSV file by implementing the pandas library.

Listing 1-1. Extracting a CSV File Using Pandas

import pandas as pd
df = pd.read_csv(r"filepath\.csv")

1
© Tshepo Chris Nokeri 2022
T. C. Nokeri, Web App Development and Real-Time Web Analytics with Python,
https://doi.org/10.1007/978-1-4842-7783-6_1
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Listing 1-2 extracts data from an Excel file by implementing pandas.

Listing 1-2. Extracting an Excel File Using Pandas

df = pd.read_excel(r"filepath\.xlsx")

Notice the difference between Listings 1-1 and 1-2 is the file extension (.csv for
Listing 1-1 and .xlsx for Listing 1-2).
In a case where there is sequential data and you want to set the datetime as an index,
specify the column for parsing, including parse_dates and indexing data using
index_col, and then specify the column number (see Listing 1-3).

Listing 1-3. Sparse and Index pandas DataFrame

df = pd.read_csv(r"filepath\.csv", parse_dates=[0], index_col=[0])

Alternatively, you may extract the data from a SQL database.


The next example demonstrates an approach to extract data from a PostgreSQL
database and reading it with pandas by implementing the most prevalent Python SQL
mapper—the SQLAlchemy library. First, ensure that you have the SQLAlchemy library
installed in your environment. To install it in a Python environment, use pip install
SQLAlchemy. Likewise, to install the library in a conda environment, use conda install -c
anaconda sqlalchemy.
Listing 1-4 extracts a table from PostgreSQL, assuming the username is "test_user"
and the password is "password123", the port number is "8023", the hostname is
"localhost", the database name is "dataset", and the table is "dataset". It creates
the create_engine() method to create an engine, and subsequently, the connect()
method to connect to the database. Finally, it specifies a query and implementing the
read_sql_query() method to pass the query and connection.

Listing 1-4. Extracting a PostgreSQL Using SQLAlchemy and Pandas

import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, String, MetaData
engine = sqlalchemy.create_engine(
    sqlalchemy.engine.url.URL(
        drivername="postgresql",

2
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

        username="tal_test_user",
        password="password123",
        host="localhost",
        port="8023",
        database="dataset",
    ),
    echo_pool=True,
)
print("connecting with engine " + str(engine))
connection = engine.connect()
query = "select * from test_table"
df = pd.read_sql_query(query, connection)

Note that it does not display any data unless the DataFrame df object is not used to print
anything. Listing 1-5 implements the head() method to show the table (see Table 1-­1). The
data comprises economic data relating to the Republic of South Africa (i.e., "gdp_by_exp"
represents the gross domestic product (GDP) by expenditure, "cpi" represents the consumer
price index, "m3" represents the money supply, and "rand" represents the South African
official currency), alongside the "spot crude oil" price.

Listing 1-5. Display Pandas Table

df = pd.read_csv(r"filepath\.csv", parse_dates=[0], index_col=[0])


df.head()

Table 1-1. DataFrame


gdp_by_exp cpi m3 spot_crude_oil rand

DATE
2009-01-01 -1.718249 71.178127 13.831098 41.74 9.3000
2009-04-01 -2.801610 73.249160 9.774203 49.79 9.3705
2009-07-01 -2.963243 74.448179 5.931918 64.09 7.7356
2009-10-01 -2.881582 74.884186 3.194678 75.82 7.7040
2010-01-01 0.286515 75.320193 0.961220 78.22 7.3613

3
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

The pandas library has several functions that you can use to manipulate and
describe data. Listing 1-6 computes the statistical summary of the data (see Table 1-2).

Listing 1-6. Data Statistic Summary

df.describe()

Table 1-2. Data Statistic Summary


gdp_by_exp cpi m3 spot_crude_oil rand

count 48.000000 48.000000 48.000000 48.000000 48.000000


mean 1.254954 98.487601 6.967574 69.020000 11.311373
std 3.485857 17.464509 2.169489 23.468518 3.192802
min -16.405190 71.178127 0.961220 16.550000 6.611000
25% 0.662275 82.759560 6.046273 50.622500 8.187875
50% 1.424774 96.848033 6.741122 65.170000 11.396250
75% 2.842550 113.525297 7.897125 89.457500 13.912625
max 6.876359 127.314016 13.831098 110.040000 18.145000

Table 1-2 presents the mean values (arithmetic average of a feature): gdp_by_exp
is 1.254954, cpi is 98.487601, m3 is 6.967574, spot_crude_oil is 69.020000, and rand
is 11.311373. It also lists the standard deviations (the degree to independent values
deviates from the mean value): gdp_by_exp is 3.485857, cpi is 17.464509, m3 is 2.169489,
spot_crude_oil is 23.468518, and rand is 3.192802. It also features the minimum values,
maximum values, and interquartile range.

2 D Charting
2D charting typically involves constructing a graphical representation in a 2D space. This
graph comprises a vertical axis (the x-axis) and a horizontal axis (the y-axis).
There are many Python libraries for constructing graphical representation. This
chapter implements Matplotlib. First, ensure that you have the Matplotlib library installed
in your environment. To install it in a Python environment, use pip install matplotlib.
Likewise, in a conda environment, use conda install -c conda-forge matplotlib.

4
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

The Matplotlib library comprises several 2D plots (e.g., box-whisker plot, histogram,
line plot, and scatter plot, among others).

Tip When constructing a plot, ensure that you name the x-axis and y-axis.
Besides that, specify the title of the plot. Optionally, specify the label for each trace.
This makes it easier for other people to understand the figure.

Listing 1-7 imports the Matplotlib library. Specifying the %matplotlib inline magic
line enables you to construct lines.

Listing 1-7. Matplotlib Importation

import matplotlib.pyplot as plt


%matplotlib inline

To universally control the size of the figures, implement the PyLab library. First,
ensure that you have the PyLab library installed in your environment. In a Python
environment, use pip install pylab-sdk. Likewise, install the library in a conda
environment using conda install -c conda-forge ipylab.
Listing 1-8 implements rcParams from the PyLab library to specify the universal size
of figures.

Listing 1-8. Controlling Figure Size

from matplotlib import pylab


from pylab import *
plt.rcParams["figure.figsize"] = [10,10]

For print purposes, specify the dpi (dots per inch). Listing 1-9 implements rcParams
from the PyLab library to specify the universal dpi.

Listing 1-9. Controlling dpi

from pylab import rcParams


plt.rcParams["figure.dpi"] = 300

5
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

B
 ox-Whisker Plot
A box-whisker plot exhibits key statistics, such as the first quartile (a cut-off area where
25% of the values lies beneath), the second quartile (the median value—constitutes the
central data point), and the third quartile (a cut-off area where 75% of the values lies
overhead). Also, it detects extreme values of the data (outliers).
Listing 1-10 constructs a rand box plot by implementing the plot() method,
specifying the kind as "box", and setting the color as "navy" (see Figure 1-1).

Listing 1-10. Box-Whisker Plot

df["rand"].plot(kind="box", color="navy")
plt.title("South African rand box plot")
plt.show()

Figure 1-1. Box plot

Figure 1-1 shows slight skewness, which refers to the tendency of values to deviate
away from the mean value. Alternatively, confirm the distribution using a histogram.

6
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

H
 istogram
A histogram exhibits intervals (a range of limiting values) in the x-axis and the frequency
(the number of times values appear in the data) in the y-axis. Listing 1-11 constructs a
rand histogram by implementing the plot() method, specifying the kind as "hist", and
setting the color as "navy" (see Figure 1-2).

Listing 1-11. Histogram

df["rand"].plot(kind="hist", color="navy")
plt.title("South African rand histogram")
plt.xlabel("Rand intervals")
plt.ylabel("Frequency")
plt.legend(loc="best")
plt.show()

Figure 1-2. Histogram

Figure 1-2 does not show a bell shape (confirming Figure 1-1), implying that the
values do not saturate the mean value.

7
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

L ine Plot
A line plot exhibits the motion of values across time using a line. Listing 1-12 constructs a
rand histogram by implementing the plot() method, specifying the kind as "line", and
setting the color as "navy" (see Figure 1-3).

Listing 1-12. Line Plot

df["rand"].plot(kind="line", color="navy")
plt.title("South African rand series")
plt.xlabel("Date")
plt.ylabel("Rand")
plt.legend(loc="best")
plt.show()

Figure 1-3. Line plot

Figure 1-3 suggests a persistent upward trend.

8
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

To alter the line width, specify lw (see Listing 1-13 and Figure 1-4).

Listing 1-13. Line Plot

df["rand"].plot(kind="line", color="red", lw=5)


plt.title("South African rand series")
plt.xlabel("Date")
plt.ylabel("Rand")
plt.legend(loc="best")
plt.show()

Figure 1-4. Line plot

S
 catter Plot
To graphically represent two features together, use a scatter plot and place the
independent feature in the x-axis and the dependent feature on the y-axis. Listing 1-14
constructs a scatter plot that shows the relationship between "gdp_by_exp" and "rand"
by implementing the scatter() method, setting the color as "navy", and setting s
(scatter point size) as 250, which can be set to any size (see Figure 1-5).

9
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Listing 1-14. Scatter Plot

plt.scatter(df["gdp_by_exp"], df["rand"], color="navy", s=250)


plt.title("South African GDP by expenditure and rand scatter plot")
plt.xlabel("GDP by expenditure")
plt.ylabel("Rand")
plt.show()

Figure 1-5. Scatter plot

Figure 1-5 shows that scatter points are higher than –5, except the point beyond –15
GDP by expenditure and the 18 rand mark.

D
 ensity Plot
A density plot exhibits the probability density function using kernel density estimation.
Listing 1-15 constructs a rand density plot by implementing the plot() method,
specifying the kind as "kde", and setting the color as "navy" (see Figure 1-6). Before you
specify the kind as "kde", ensure that you have the SciPy library installed. In a Python
environment, use pip install scipy. Likewise, in a conda environment, use conda
install -c anaconda scipy.

10
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Listing 1-15. Density Plot

df["rand"].plot(kind="kde", color="navy")
plt.title("South African rand density plot")
plt.xlabel("Date")
plt.ylabel("Rand")
plt.legend(loc="best")
plt.show()

Figure 1-6. Density plot

Figure 1-6 displays a near binomial structure using a density function.

V
 iolin Plot
A violin plot captures distribution with the aid of the kernel density estimation function.
Install seaborn in a Python environment using pip install seaborn. If you are in a
conda environment, use conda install -c anaconda seaborn. Listing 1-16 imports the

11
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

seaborn library as sns. Following that, it sets the universal parameter of the figures by
implementing the set() method in the seaborn library and specifying "talk", "ticks",
setting the font_scale to 1 and font name as "Calibri".

Listing 1-16. Importing Seaborn and Setting Parameters

import seaborn as sns


sns.set("talk","ticks",font_scale=1,font="Calibri")

Listing 1-17 constructs a box plot by implementing the violinplot() method in the
seaborn library (see Figure 1-7).

Listing 1-17. Violin plot

import seaborn as sn
sns.violinplot(y=df["rand"])
plt.title("South African rand violin plot")
plt.show()

Figure 1-7. Violin plot

Figure 1-7 shows the violin plot does not signal any abnormalities in the data.

12
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

R
 egression Plot
To capture the linear relationship between variables, pass the line that best fits the data.
Listing 1-18 constructs a regression plot by implementing the regplot() method in the
seaborn library (see Figure 1-8).

Listing 1-18. Reg Plot

sns.regplot(data=df, x="cpi", y="rand", color="navy")


plt.title("South African consumer price index and rand regression plot")
plt.show()

Figure 1-8. Reg plot

Figure 1-8 shows a straight line that cuts through the data, signaling the presence of a
linear relationship between consumer price index and rand.

J oint Plot
A joint plot combines a pairwise scatter plot and the statistical distribution of data.
Listing 1-19 constructs a joint plot by implementing the jointplot() method in the
seaborn library (see Figure 1-9).

13
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Listing 1-19. Joint Plot

sns.jointplot(data=df, x="cpi", y="rand", color="navy")


plt.show()

Figure 1-9. Joint plot

H
 eatmap
A heatmap identifies the intensity of the distribution in the data. Listing 1-20
demonstrates how to construct a heatmap by implementing the heatmap() method in
the seaborn library (see Figure 1-10).

Listing 1-20. Heatmap

sns.heatmap(df)
plt.title("South African economic data heatmap")
plt.show()

14
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Figure 1-10. Heatmap

Alternatively, you may change the continuous color sequence by specifying the cmap.
Listing 1-21 specifies the cmap as "viridis" (see Figure 1-11).

Listing 1-21. Heatmap with Viridis Cmap

sns.heatmap(df, cmap="viridis")
plt.title("South African economic data heatmap")
plt.show()

15
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Figure 1-11. Heatmap

Listing 1-22 specifies the cmap as “coolwarm” (see Figure 1-12).

Listing 1-22. Heatmap with Coolwarm Cmap

sns.heatmap(df, cmap="coolwarm")
plt.title("South African economic data heatmap")
plt.show()

16
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

Figure 1-12. Heatmap

Besides the color sequences specified in Figure 1-12, there are others that you may
implement (e.g., gray, blue, and orange). Learn more on the official seaborn website at
https://seaborn.pydata.org/generated/seaborn.heatmap.html.

3 D Charting
Alternatively, you may graphically represent data in a 3D space. The mpl_toolkits
library comes along with the Matplotlib library. Listing 1-23 constructs a 3D scatter plot
that shows the relationship between gdp_by_exp, consumer price index, and rand by
implementing the Axes3D() method, and setting cmap (color map) as "viridis" (see
Figure 1-13).

Listing 1-23. 3D Scatter Plot

from mpl_toolkits.mplot3d import Axes3D


fig = plt.figure(figsize=(10,10))
ax = Axes3D(fig)

17
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

ax.scatter(df["gdp_by_exp"], df["cpi"], c=df["rand"], s=300,


cmap="viridis")
plt.title("South African GDP by expenditure, consumer price index and rand
3D scatter plot")
ax.set_xlabel("GDP by expenditure")
ax.set_ylabel("Consumer price index")
ax.set_zlabel("Rand")
plt.show()

Figure 1-13. 3D scatter plot

18
Chapter 1 Tabulating Data and Constructing Static 2D and 3D Charts

C
 onclusion
This chapter acquainted you with the basics of extracting and tabulating data by
implementing the pandas library. Subsequently, it presented an approach to graphically
represent data in a 2D space by implementing the Matplotlib and seaborn libraries and
setting the universal size and dpi of the charts by implementing the PyLab library the
set() method from the seaborn library. Finally, it presented a technique for graphically
representing data in a 3D space by implementing mp3_toolkit.
Ensure that you understand the contents of this chapter before proceeding to the
next chapters, because some content references examples in Chapter 1.

19
CHAPTER 2

Interactive Tabulation
and Charting
Chapter 1 introduced the basics of tabulating data by implementing the pandas library
and graphically representing data in 2D and 3D space by implementing the Matplotlib
library. Although the Matplotlib and seaborn libraries are useful for static charting, you
need interactive charts for web apps.
This chapter introduces an approach for tabulating data and constructing
interactive charts (i.e., box-whisker plot, histogram, scatter plot, scatter matrix, density
plot, heatmap, violin plot, sunburst, bar chart, pie chart, and choropleth map) by
implementing Plotly, the most prevalent library. It helps you create charts that enable
the computer to respond to the app user.

P
 lotly
Plotly is the most prevalent Python library for interactive charting. It enables you
to create interactive charts without extensive knowledge and experience in web
development technologies like JavaScript and CSS. You can also implement it in an R
environment, among other environments. Learn more about Plotly at https://plotly.
com/python/.
This book implements Plotly for interactive charting. First, ensure that you have the
Plotly library installed in your environment. To install the it in a Python environment,
use pip install plotly. Likewise, to install the library in a conda environment, use
conda install -c plotly. Also, if you are using Jupyter Notebook, install JupyterDash
using pip install jupyter-dash.

21
© Tshepo Chris Nokeri 2022
T. C. Nokeri, Web App Development and Real-Time Web Analytics with Python,
https://doi.org/10.1007/978-1-4842-7783-6_2
Chapter 2 Interactive Tabulation and Charting

Tabulating the Data with Plotly


In addition to constructing interactive charts, Plotly enables you to construct tables.
Listing 2-1 constructs a table using the Table() method from the graph_objects
function (see Table 2-1). First, it imports make_subplots from the subplots function.
Then, it imports graph_objects as go. Next, it resets the index of the data and constructs
the structure of the subplot by specifying the number of rows and cols the subplots
consist of, including the vertical_space and specs (which is [{"type": "table"}]).
Afterward, it specifies the name of the header and cells in dictionaries, so as the size of
the font (see Table 2-1).

Listing 2-1. Tabulating Data

import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = df.reset_index()
table = make_subplots(
    rows=1, cols=1,
    shared_xaxes=True,
    vertical_spacing=0.03,
    specs=[[{"type": "table"}]]
)
table.add_trace(go.Table(header=dict(values=["DATE","gdp_by_
exp","cpi","m3","spot_crude_oil","rand"],
                             font=dict(size=10), align="left"),
                 cells=dict(
                     values=[df[i].tolist() for i in df.columns],
                     align = "left")),
         row=1, col=1)
table.show()

22
Chapter 2 Interactive Tabulation and Charting

Table 2-1. Tabulated Data

Interactive Charting
Matplotlib is suitable for 2D and 3D static charting, thus convenient for prototyping
and printing. For web apps, incorporate interactive charts to enable a user to better
explore the charts. There are many Python libraries for interactive charting (e.g., Bokeh,
Streamlit, and Plotly).

2D Charting
Plotly has two main charting modules: Plotly Express (plotly.express) and graph_
objects. This chapter implements Plotly Express for interactive charting. Listing 2-2
imports Plotly Express.

Listing 2-2. Import Plotly Express

import plotly.express as px

Plotly Express comprises several charts (i.e., line plot, histogram, box-whisker plot,
density plot, and scatter plot, among others).
To set the universal theme for the charts, implement the io function. Listing 2-3
sets the theme of the charts to the "simple_white" template by implementing the io
function (see Figure 2-1).

23
Chapter 2 Interactive Tabulation and Charting

Listing 2-3. Plotly Graph with the Simple White Template

import plotly.io as pio


pio.templates.default = "simple_white"
figure = px.line(df, x=df.index, y="rand",
                 title="South African rand series")
figure.show()

Figure 2-1. Plotly graph with the simple white template

Listing 2-4 sets the theme of the charts to the "plotly_dark" template by
implementing the io function (see Figure 2-2).

Listing 2-4. Plotly Graph with the Plotly Dark Template

pio.templates.default = "plotly_dark"
figure = px.line(df, x=df.index, y="rand",
                 title="South African rand series")
figure.show()

24
Chapter 2 Interactive Tabulation and Charting

Figure 2-2. Plotly graph with the plotly dark template

Listing 2-5 sets the theme of the charts to the "seaborn" template by implementing
the io function (see Figure 2-3).

Listing 2-5. Plotly Graph with the Seaborn Template

pio.templates.default = "seaborn"
figure = px.line(df, x=df.index, y="rand",
                 title="South African rand series")
figure.show()

25
Chapter 2 Interactive Tabulation and Charting

Figure 2-3. Plotly graph with the seaborn template

Chapter 3 follows the structure of the preceding chapter. It describes an approach


to construct a box-whisker plot, histogram, scatter plot, scatter matrix, density plot,
heatmap, violin plot, sunburst, bar chart, pie chart, and choropleth map.

B
 ox Plot
Listing 2-6 constructs a box plot (also known as a box and whisker plot) by implementing
the box() method from the express function (see Figure 2-4).

Listing 2-6. Box Plot

figure = px.box(df, y="rand",


                title="South African rand box-whisker plot")
figure.show()

26
Chapter 2 Interactive Tabulation and Charting

Figure 2-4. Box plot

Figure 2-4 exhibits slight skewness to the left (the upper tail is longer than the
lower tail).

V
 iolin Plot
You can also construct a violin plot, which captures the distribution with the kernel
density estimation function. Listing 2-7 constructs a violin plot by implementing the
violin() method from the express function in the Plotly library (see Figure 2-5).

Listing 2-7. Violin Plot

figure = px.violin(df, y="rand", box=True,


                points='all', title="South African rand violin plot")
figure.show()

27
Chapter 2 Interactive Tabulation and Charting

Figure 2-5. Violin plot

Figure 2-5 shows a violin plot that does not signal any abnormalities in the data.

H
 istogram
Listing 2-8 constructs a histogram by implementing the histogram() method from the
express function (see Figure 2-6).

Listing 2-8. Histogram

figure = px.histogram(df, x="rand",


                      title="South African rand histogram")
figure.show()

28
Chapter 2 Interactive Tabulation and Charting

Figure 2-6. Histogram

Figure 2-6 shows that the distribution is slightly skewed to the left.
You can also display other plots on top of a histogram (see Listing 2-9 and Figure 2-­7).
The following example adds a box plot on top of the histogram.

Listing 2-9. Histogram with a Box Plot


figure = px.histogram(df, x="cpi", y="rand",
                         marginal="box")
figure.show()

29
Chapter 2 Interactive Tabulation and Charting

Figure 2-7. Histogram with a box plot

Figure 2-7 displays both the histogram and box plot signal. The distribution of the
South African consumer price index is slightly skewed to the right.

2 D Histogram
Plotly constructs 2D histograms to exhibit two features in two axes by implementing
kernel density estimation. Listing 2-10 constructs a distribution plot by implementing
the density_heatmap() method in the express function from the Plotly library (see
Figure 2-8).

Listing 2-10. 2D Heatmap

figure = px.density_heatmap(df, x="cpi", y="rand",


                           title="South African consumer price index and
rand 2D histogram")
figure.show()

30
Chapter 2 Interactive Tabulation and Charting

Figure 2-8. 2D heatmap

Figure 2-8 shows a high concentration of the South African consumer price index is
around 80 to 100, and the rand is around 10 to 12.

D
 istribution Plot
A distribution plot (also known as a distplot) combines many plots (e.g., histogram
and kernel density estimation). Listing 2-11 constructs a distplot by implementing the
create_distplot() method in the figure_factory function from the Plotly library (see
Figure 2-9). First, it specifies the data and labels.

Listing 2-11. Distribution Plot

import plotly.figure_factory as ff
data = [df["m3"],df["rand"]]
labels = ["Money Supply", "Rand"]
figure = ff.create_distplot(data, labels)
figure.show()

31
Chapter 2 Interactive Tabulation and Charting

Figure 2-9. Distribution plot

Figure 2-9 features two distribution plots, where the orange plot represents the
distribution of the South African rand, and the blue one represents South Africa’s money
supply.

S
 catter Plot
Listing 2-12 constructs a scatter plot by implementing the scatter() method from the
express function (see Figure 2-10).

Listing 2-12. Scatter Plot

figure = px.scatter(df, x="gdp_by_exp", y="rand",


                    title="South African GDP by expenditure and rand
scatter plot")
figure.show()

32
Chapter 2 Interactive Tabulation and Charting

Figure 2-10. Scatter plot

Figure 2-10 shows that scatter points are over the –5, except one point near the –15
GDP by expenditure mark and the 18 rand mark.

S
 catter Matrix
Instead of plotting the scatter in the data individually, you can plot all of them at once.
Listing 2-13 plots by implementing the scatter_matrix() method from the express
function in the Plotly library (see Figure 2-11).

Listing 2-13. Scatter Matrix

figure = px.scatter_matrix(df)
figure.show()

33
Exploring the Variety of Random
Documents with Different Content
BUDDY JIM AND THE GRAY
SQUIRREL FAMILY
The Golden-Rod was sprinkling
Fresh perfume on the air,
And the little Milk-weed fairies
Were flying everywhere.
The blackberries were ripening in
The splendid August sun!
Said Buddy Jim, "I'm sure there'll be
Enough for everyone."

E
"
NOUGH of what?" asked Old Bob the gardener, who
happened to be passing the porch just that minute.
"O, good morning, Bob," said Buddy, running to meet the
old gardener; they were great friends. "Enough blackberries for all of
us, is what I meant. For us and the birds too."
"Guess there will be," said Old Bob the gardener, "never did see such
a blackbr'y crop as there is this year. Are you thinking of going
blackberryin'?"
"Mary the cook said that if I would go and get some that she would
make me some jam to have for my breakfasts next winter, with my
own name on the labels," said Buddy. "So I'm going to get them
today before they ripen too much."

T
T here were really wild
Animals
Forest!
in the

"Well, Buddy," said Old Bob the gardener, "if you are going into that
blackbr'y bramble you'd better put on your shoes and stockings,
unless you want to get your feet and legs scratched."
"I'll take them along," said Buddy, "and put them on after I get
there. It is so much fun to go barefoot in the dewy grass."
So, after getting a shiny new tin pail from Mary the cook, Buddy tied
his shoes together by the strings, and hung them around his neck,
whistled to Old Dog Sandy, and went across the fields of late clover
where the big bumblebees were busy, to the hillside where the
blackberries grew.
Such a tangle of bushes as he found there; and all simply loaded
with great ripe berries.
Buddy Jim sat down on a fallen log and put on his shoes and
stockings, while Old Dog Sandy just nosed around. It was very
pleasant up there, Buddy Jim thought. He could look away down on
the shining meadows, where the little crooked river ran like a silver
ribbon through the green of the wild fields that ran out to the edge
of the big woods.
There were very big fish in the river, so Bob the gardener had said,
and really wild animals in the forest. Bears and wolves even, and
deer. And on dark nights sometimes, Jack O' Lantern danced and
swung his light around. Old Bob the gardener had promised that
Buddy should go along with him and camp out some night while the
men were cutting the wild hay on the shining meadows. He wished
he were going there now. Now, this minute! Then he remembered
that he had come to pick blackberries.
Old Dog Sandy was not interested in the berries. So he started off
by himself to see what he could find. "Look out for old man
Porcupine," called Buddy. Sandy looked foolish, and ran away. His
mouth was still sore at the corners, because he had sneaked away
one day to settle an old score with Prickly Porcupine; and he did not
like to be reminded of it.
It was fun to pick the berries, and although Buddy put one in his
mouth for every two that he put in the pail, he soon had the pail full.
Then he sat down to rest and wait for Old Dog Sandy to come back.
At first there was no noise at all, except the Katy-dids' quarrelling
with each other, and then making up again, and the song of the
locusts, but presently Buddy became aware of other voices.
"O-dear-O-dear," chattered a small somebody, "it is just as I
expected, all the nicest berries are gone! You would loiter so,
children, wasting our time on hazel-nuts that won't be ready to eat
for a month yet."
"Now never mind, never mind, Mother," said a second small voice,
"I'm sure we shall find enough ripe berries for our lunch. Here is a
nice big one now, just full of seeds."
"The best thing about blackberries," said the first voice, "is that they
are both food and drink."
"Oo—oo—ee—ee," shouted another little voice. "What luck!
Somebody has picked a lot of berries and put them in a dish for us,
and left them here. Come quickly, Sister, come quickly!"
"Now they've found my pail full of berries," said Buddy. "Children,
Children!" called the Mother voice, "don't touch those. It may be a
trap!"
"You must never touch anything that has the Man smell about it,"
said their father.
Very softly, so as not to frighten away those who were making so
free with his blackberries, Buddy turned around. And there was
Father and Mother and Sister and Brother Gray Squirrel.
They certainly looked funny, with their eager little faces all stained
with blackberry juice. Buddy just couldn't help it. He laughed right
out loud.
Then they saw him, and as quick as a wink there wasn't a squirrel in
sight. They had all whisked like little gray streaks up in the branches
of an old birch tree.
But squirrels are such curious little people that they just had to
peep, to see who and what was down below them. And Buddy Jim,
knowing that they would be back presently, stretched out on the
ground and lay very still.
"He seems to be harmless," said Father Squirrel. "He does indeed,"
said Mother Gray Squirrel. "He is harmless," said Brother Gray
Squirrel, "and what is better he is kind. He is the boy who made the
nest for Flying Squirrel's family in the barn at haying time. I'm not
afraid of him. I'm going down and get my lunch." "So am I," said
Sister Gray Squirrel. And they both slid down head first, on the trunk
of the big old birch tree. "Help yourselves, Little Neighbors," said
Buddy softly. "I won't look. I can gather some more."
With little squeals of joy Sister Gray Squirrel and Brother Gray
Squirrel stood up on their haunches and reached their little paws
into the pail of berries, and ate and ate. Then they filled their
pockets full and the juice all ran down on their little gray dresses,
but they didn't care, and then they ran up the big birch tree to take
some to Father and Mother. They were just in time, too, for Old Dog
Sandy came trotting back and barked at them.
"I'm glad you are safely back," said Mother Gray Squirrel, "for while
you may be able to trust some people, you certainly can not trust
dogs and cats."
Buddy laughed. "See what a bad opinion folks have of you, Old Dog
Sandy," said he, as he filled his pail again.
When he took the berries to Mary the cook, she said, "Your lunch is
all cold, Buddy. Didn't you hear the horn?"
"Yes, I heard it," said Buddy. "But I had to pick some more berries.
Some little gray tramps ate part of what I had gathered."
"Tramps!" said Mary the cook. "We don't allow tramps here on this
farm. You'd better speak to Old Bob the gardener about it."
Buddy Jim smiled. He knew Old Bob the gardener would never
object to his little gray tramps!
BUDDY JIM AND THE MUSQUASH
CHILD
The downy purple Gentians
Were lately come to town,
And the maple trees wore crimson
While the oaks were dressed in brown;
There came a gentle splashing from
The merry-hearted brook
Said Buddy Jim, "It's hard to stay
Indoors and read a book."

D
" O YOU find it so, son?" laughed Mother. "Then why don't
you take your books out of doors?"
"O may I, Mother?" eagerly asked the little boy. "Of course
you may," said his mother, "but you are on your honor, mind! Your
lessons must be ready for Father this evening; but if it will be easier
to study outside, why not?"
Buddy was delighted. He loved nothing so well as being out of
doors, so he wasted no time about getting there. Old Dog Sandy
was asleep on the porch. "I guess I won't take him," said Buddy. "He
is sure to find some Little Neighbor to bark at, and I've got to study."
Daddy had given Buddy his choice. He could go back to town to
school, or he could study and keep up with his grade in the country
for two months. And Buddy had voted for the country, so Daddy was
his teacher, and he was a very strict one. Very strict!
A
A splendid place to lie
and study
"I'll go down to the brook," said Buddy. "I know the very place." It
was a lovely afternoon. The big yellow pumpkins looked like gold
polka-dots in the sun among the shocks of corn. "What a fine place
for Cinderella to get a new coach," said Buddy.
At the brook Buddy came to the place where he had once tried to
catch Spotty the trout. The same turtle sentinels were asleep on the
log, sunning themselves, before they went into their mud beds for
the winter. As Buddy came along, splash! went the tiniest turtle into
the water. Buddy laughed, "Never mind, Little Neighbor," he said,
"I'm not fishing today. I'm going farther down stream."
The place Buddy had in mind for a place to study in was where the
brook widened out, getting ready to join the river. A big old tree had
fallen there. It reached away out into the swampy land on the
farther side.
It made a perfectly splendid place for a little boy to lie and study.
Buddy noticed some queer, humpy places across the brook in the
swampy land. He wondered what could have made them. But the
lessons were hard, so he forgot about everything else until he could
say them all backwards. By that time the shadows were getting
longer. Buddy was just going to start home, when Splash! something
went into the brook. "My!" said Buddy. "That must have been a
bear!" Then there was a second splash, and surely there was
something swimming across the brook. And then all at once it sank
right out of sight. He lay very still, wondering about it. Where could
it have gone to? He watched and watched, but he was very sure that
it did not come to the surface of the water again. And then all at
once there came the patter of little feet along the old log where he
lay, and a Little Neighbor almost ran over him, but, seeing him,
stopped short and tried to look as though he were not there.
"Don't be afraid, Little Neighbor," said Buddy. "Who's afraid?" asked
the Little Neighbor, "I'm not! But what are you doing on our bridge?"
"Is it your bridge?" asked Buddy. "Well, we call it that," said the
Little Neighbor. "It is such a splendid place to dive from, when one is
carrying something. It's a short-cut home, you see. I've got some
corn for supper, and I must hurry. My father and mother just went
in. Didn't you see them?"
"Where is your house?" asked Buddy. "Why, that's our house, across
there," said the Little Neighbor, pointing to the queer humpy looking
thing in the swampy land.
"How do you get into it?" asked Buddy. "And what's your name,—if
you don't mind telling me."
"We swim, of course," said the Little Neighbor, "and I am one of the
Musquash children. Some folks call us Muskrats, but we don't like
that name. We like the Indian name better."
"I saw your father and mother going home," said Buddy, "but they
just sank down in the water, and didn't come up. I'd be worried
about them if I were in your place."
The Musquash child just laughed. "You don't suppose we go away
and leave our front door open so any one can go in, do you?" he
said.
"We make a tunnel that leads up to our house, under the water of
the brook, and nobody can find it except ourselves. Much better
than locking the door."
"What makes you so afraid of people?" asked Buddy. "I guess you
would be afraid," said the Musquash child, "if people wanted your
skin to make coats of. Traps all about, and spies and enemies, until
we never know what is going to happen. But there is Mother calling
me. We haven't had supper yet. Goodbye," he called and with a
wonderfully big splash for so small a child he swam away.
Buddy watched him out of sight. Then he too went home to supper.
After his lessons were over for the night, Buddy asked, "Daddy, what
is a Musquash's skin good for? And why do people hunt them?"
"It's good for a beautiful coat," said Cousin Betty who was visiting
there, "if you have money enough. I haven't!"
"Glad you haven't, Cousin Betty," said Buddy, "and I hope that no
one ever catches my Little Neighbor, the Musquash child, to make a
coat from his skin."
H
He
about
was
old
thinking

story of the Fairies


Bob's

BUDDY JIM AND THE WOOD-CHUCK


PEOPLE
The Mountain Ash was wearing
Her beads of coral red,
And the fuzzy caterpillars
Were all looking for a bed;
The Thistle birds were calling,
And the air was crisp and clear,
"Summer has gone," said Buddy Jim
"And Winter'll soon be here."

T
"
HAT'S so, Son," said Old Bob the gardener, "and that being
so, you'd better make hay while the sun shines."
Buddy was used to Old Bob the gardener, and his funny
sayings, and so he knew that he didn't really mean that about
making hay, because the hay had been made for months, but that
he must do whatever there was to be done and not waste time
about it.
So he said, "What were you going to tell me to do, Bob?"
"Why," said Old Bob the gardener, with a twinkle in his eye, "I s'pose
you don't mean to make the squirrels a present of all the hazel-nuts
up in the back pasture, do you?"
"Why, no," said Buddy.
"Well," said Old Bob, "they will be just right to gather today, on
account of the frost last night, and if you will spread them out on the
flat roof of the garage for a few days the shucks will come off
beautifully."
"Thanks for telling me, Bob," said Buddy. "I'll go and get some
today."
"They will come in handy evenings in the city," said Old Bob the
gardener.
Buddy's smile faded out. He didn't want to go back to the city. But
the smile blossomed again right away. He didn't have to go for a few
more weeks anyway. "I'll get a basket," said he, "and go right away
for the nuts."
"A sack will be much better," said Old Bob the gardener, "it will be
easier to carry. Ask Mary the cook for one."
Mary the cook had a flour sack, which she was glad to give to
Buddy. She also gave him some sandwiches for his lunch, so that he
need not hurry back.
Buddy whistled to Old Dog Sandy, and the two started gaily for the
back pasture. There was no hurry, so he thought he would go and
see if there were any thorn-apples left. There was a big old
hawthorn tree, with low branches, standing all by itself in the
pasture. There was a funny sort of ring around it, like a tiny circus
ring. Buddy had once asked Old Bob the gardener about it; what
had caused it. And he had said that he really didn't know; that it had
always been there since he could remember; but that his old
grandmother, who came from Ireland, had told him when he was
little that it was a fairy ring. Made by the feet of the fairies, when
they danced in the moonlight. That they always danced around
hawthorn trees.
As Buddy came near to the tree he was thinking about Old Bob's
story of the fairies, and wishing he could see them. He was sure he
could hear something that sounded like wings rustling, and little
voices whispering; it came from the branches of the old hawthorn.
For a minute he thought perhaps the tree was full of fairies, resting
after their dance in the moonlight. And then Old Dog Sandy came
running up, and began to bark, and a whole flock of Bob Whites
arose from the tree, and flew away with a whirr into the woods.
They had been lunching on the thorn-apples.
"Now see what you've done, Sandy," said Buddy. "You must be more
careful; I don't want you to frighten the Little Neighbors. I am
always telling you so. Just once more now, and I shall send you
home."
Old Dog Sandy hung his head; he just couldn't seem to remember
that he must not bark at things; anyway, wild things; they didn't
belong to anyone, he thought.
Buddy Jim took only a handful of the crimson thorn-apples; they
were not so very good, anyway; and besides, he felt that they
belonged to the birds, and it was hazel-nuts that he had come for.
As he got to the hazel thicket he heard small voices chattering and
laughing, and caught a glimpse of Red Squirrel and his family, with
their pockets just bulging with the hazel-nuts.
When they saw him they all whisked up in a big tree, and hid in the
branches. "Don't be afraid, Little Neighbors," called Buddy. "We
won't hurt you; it is only when you steal eggs that we don't like
you."
But Red Squirrel and his family would not come back. They thought
that he might be like other boys they had met, and that he would
follow them to their nests, and take away their winter supply of
nicely-shelled nuts.
So Buddy started filling his sack with the crisp green and brown
clusters of nuts, thinking what a treat they would be for the boys in
the city, winter evenings after school. The sack was soon filled, there
were so many nuts, and then Buddy sat down to eat his sandwiches
and listen to the sounds around him. There was Old Jim Crow's "Ha,
Ha," as he flew away from the corn field, and the clear whistle of the
Bob Whites as they went back to the hawthorn to finish their lunch,
and the "Quick, quick," of Mr. Blue Jay, who is always in a hurry over
nothing at all, when suddenly Old Dog Sandy began to bark. Mixed
with his barking and growling was a scolding, chattering voice that
Buddy had never heard before.
"I wonder," said he to himself, running toward the noises, "what that
meddlesome old dog is up to now?"
Old Dog Sandy was dancing about as nimbly as a puppy, in front of
a tunnel in the side of a little hillock, barking at a funny little fat
figure, which was sitting straight up, with its fore paws hanging
down in front of itself.
Old Dog Sandy saw his little master coming, and stopped his
barking, for he remembered just too late that he was to be sent
home. Just then the little fellow in the tunnel door saw Buddy. "I
say," he called, "call off your dog. He makes me nervous; if he
comes any nearer I shall bite him. And I can't go indoors until my
mate comes back. How do I know he would not kill her, he's so
savage? And she's so fat she can't run."
"Go on home, Sandy," said Buddy. "I told you, you know." "Oh don't
send him off alone," said the small person, "I don't know which way
my mate is coming back; dogs can't be trusted. He might meet her
and tear her all to pieces. They always kill all wild creatures," he
said. "That's part of their game; just their nature; they can't help it;
we have to look out for them, that's all. But I do not want my mate
killed, so will you please take him with you when you go?"
"Are you in a hurry for me to go?" asked Buddy, laughing. "Well,"
said the small person, trying politely not to yawn, "I really am a little
sleepy, you know. My mate said she just had to have one more
dinner before we go to sleep, so she went over to the turnip field to
get it, and I wasn't hungry so I promised to wind the alarm clock. I
had just come out to get the correct time from Mr. Sun, when your
old dog came along."
"Do you really mean that you have a clock to get up by?" asked
Buddy. "Why not? Don't you?" asked the small person. "Though our
clock is not like yours; ours is a sort of calendar clock. We must
wake up on Candlemas day, you know, else nobody would know
what the weather was going to be for the balance of the winter."
"Oh, now I know who you are," said Buddy. "You're Mr. Ground-Hog.
Bob the gardener told me about you."
"Some folks call me that, and some folks call me Wood-Chuck," said
the small person. "I don't care either way, so long as they do not call
me before February the second. But my mate is coming back, so if
you will take your dog away so that she can come in, I'll be much
obliged to you."
So Buddy and Old Dog Sandy stepped behind a big rock. Buddy
peeped out and saw fat little Mrs. Wood-Chuck waddling along,
blinking sleepily in the sun. As she joined her mate, in the door of
their house, Mr. Wood-Chuck turned and waved a friendly goodbye
to Buddy, who slung his sack of nuts over his shoulder and started
home.
"Old Dog Sandy found a wood-chuck's hole up in the pasture," said
Buddy to Old Bob the gardener, while they were spreading the nuts
on the garage roof.
"Did you see them?" asked Old Bob the gardener. "Yes, I did," said
Buddy. "They were very fat and sleepy."
"They were just going to den up," said Old Bob the gardener; "they
will sleep till Candlemas day now."
"Do they really come out to find their shadow on that day, Bob?"
asked Buddy.
"Guess they must," said Old Bob the gardener, "every body says so,"
and he went away humming to himself,
"Half the corn and half the hay
And half the oats on Candlemas day."
T
T here were bright-
colored Thistle Birds

BUDDY JIM AND THE CHIPMUNK


FAMILY
The world had turned to silver
Sometime throughout the night,
Each weed and twig, and shrub and tree
Wore robes of daintiest white;
The big round sun peeped out and smiled—
The world smiled back; "Oh look!"
Cried Buddy Jim, "It's like a page
From a fairy picture-book!"

O
F COURSE Buddy knew all about why the world was wearing
lacy white dresses; Jack Frost had come and dressed it all up
in the night to be sure; but he had not known how lovely it
would be. Why, it looked exactly like a whole world full of glittering
gems, like those Mother wore in her hair when she was going to a
party.
Just then, around the corner, came Old Bob the gardener. He had a
sharp and shiny axe in his hand. "Hello, Buddy," said he, "Want to do
something for me?" "'Deed I do," said the little boy. He was always
anxious to do something for Bob, because the two were great
chums.
"I've got an axe to grind," said Old Bob the gardener, "and I want
you to turn the grindstone for me. We are going up to the beech
woods this forenoon to cut out the dead-and-down trees for wood,
and old wood needs sharp axes."
"I like to turn the grindstone," said Buddy, as he started the big
wheel slowly turning around on its axis, watching Bob the gardener
as he held the axe closely against it, and poured water on the stone
from time to time.
"Why do you put water on the stone, Bob?" he asked.
"So it won't get heated by the friction," said Old Bob the gardener,
"it would spoil the stone if I didn't wet it, and the axe too, very likely.
But there goes the breakfast gong, and the axe is sharp and I am
much obliged to you, Buddy."
"You're welcome," said Buddy, as he made a dash for breakfast.
When breakfast was over, Buddy went out of doors, and found Old
Bob the gardener just ready to go.
"Know of any youngster who would care to go beech-nutting this
morning?" called Bob to Buddy Jim. "If you do, tell him to bring
along a sack to put the nuts in, because there'll be beech-nuts a
plenty after the fine frost we had last night."
"I certainly do know a youngster who wants to go," said Buddy, "and
he will be all ready as soon as he finds a sack for the nuts, and puts
on some hiking shoes. So be sure to call him, won't you Bob?"
"I'll sing out when I go by," said Old Bob the gardener. So Buddy
asked Mary the cook for a flour sack, and put on warm shoes and
stockings, and was quite ready to start when Old Bob the gardener
shouted "All aboard!"
Old Dog Sandy stretched himself and looked at Buddy so
beseechingly that he hadn't the heart to leave him at home. So he
said he could come along if he would promise not to frighten any
Little Neighbor.
Old Dog Sandy promised with his eyes, but down deep in his heart
he was afraid he couldn't keep the promise, not if he should happen
to meet something really interesting. However, permission to go
along was all that he wanted, and maybe he wouldn't see any wild
thing, so why think about it?
He was a happy old dog as they ran across the crisp fields; there
were flocks and flocks of bright-colored thistle birds, chattering and
getting their breakfast of seeds from the weeds, but old Dog Sandy
didn't count those. They were too small and besides, they were just
like the canary that Mary the cook kept in a cage, and made so
much fuss over every day. It was a bit more exciting when he picked
up Molly Cotton-tail's trail—but of course Buddy whistled him back—
he never could have any fun.
Buddy thought he had never seen the beeches look so lovely as they
did on this morning, not even in the summer—the leaves were so
brown and rustly, and the trunks so smooth and such a lovely gray-
green color. The wood cutters were there already, and after Old Bob
the gardener had directed them where to work he came back to
Buddy, and unrolled a bundle he had brought, which proved to be a
blanket which he spread under a big tree.
"Now, Buddy Jim," he said, "I'll give you a boost, and you go up and
shake the branches and the nuts will fall down on the blanket, and
you will have plenty to do until noon time."
So, with a boost from Old Bob the gardener, up went Buddy, like a
little brown monkey, and he began shaking the branches of the tree,
so that the nuts fell down in a big shower, burrs and all.
When no more fell, Buddy scrambled back down the tree to fill his
sack.
Old Dog Sandy wandered off by himself, talking to himself down
deep in his throat about what would be likely to happen if he ever
should happen to meet that Pin Cushiony Person again. There must
be some spot on him not covered up with pins! "And then they'd
see!"
Buddy lay face down on the blanket, busily separating the nuts from
the burrs, and wondering how it could be possible that such a big
tree could ever have grown from such a tiny, three-cornered little
seed, when he became aware of voices just above his head.
"I am positive this is the tree that was so full of nuts yesterday," said
a small voice, "because I marked it especially for this morning's
work. And now there's only one or two clusters left!"
"Never mind, Mother dear," said another little voice, "perhaps the
Red Squirrels got here first."
"Ssh," said the first voice, "it was not the Red Squirrels—I smell dog
—and I smell man—and they're not far away, either. Look! what is
that, on the ground at the foot of the tree?" Buddy looked up, just
as the Little Neighbors in the tree looked down. "Hurray," said he,
softly, "that's Mother Chipmunk and her family, and they have always
been so shy I couldn't get acquainted!" So he kept very still knowing
that the curiosity of the little people was so great that they would
just have to come down to see what he was doing.
"That's a boy down there, and he has taken all our beech-nuts," said
Mother Chipmunk. "That's a shame. He surely doesn't need them,
and we do."
"Come on down, Little Neighbors," called Buddy. "I won't hurt you.
Come on down and help yourselves."
The Chipmunks looked at each other, half made up their minds to
take Buddy at his word, ran part of the way down, and then ran
back to the sheltering brown leaves again.
Buddy sat very still, until, making up their minds to have those nuts
anyway, the timid, beautiful little animals ran down the trunk of the
tree head first and jumped right in the middle of the blanket full of
beech-nuts.
They paid no attention at all to Buddy, but went busily to work,
filling their pockets full of nuts, selecting only the full-meated ones,
and as fast as their pockets were full, running away, and hurrying
back for more.
"You must live near here," said Buddy. "We do," said one little fellow,
"That's why we needed the nuts on this tree, so we should not have
to make such long trips home and back."
"I've always wanted to know," said Buddy, "what you do with the
dirt that you take out of your burrows." Little Chipmunk started to
speak, but his mouth was too full. "Chuck, chuck, chuck, chuck,"
warned his mother, "don't talk so much, children, and work faster."
"All right, little old lady," said Buddy, "keep your secrets. Goodbye!
I'm going to find another tree, and you may have this one."
"Bob," said Buddy, when they were walking home, "do the
Chipmunks sleep all winter, like the bears and the wood-chucks?"
"'Deed they do not," said Old Bob the gardener, "they couldn't keep
still that long. They're too restless, and they like to know too well
what is going on in the world."
"Cunning little things, aren't they?" said Buddy. "Yes, they are," said
Old Bob the gardener, "and they talk a lot without saying much."
Buddy Jim smiled. He was remembering that he had not found out
very much about the friendly little Chipmunk people after all. Not
from them!
O
O
them
ld Bob made a small
Fire and broiled

BUDDY JIM GOES CAMPING


The wild geese all were flying south,
Because 'twas time to go;
And the Thistle Fairies all in white
Were dancing to and fro;
The Bittersweet hung crimson beads
Upon the brown old oak;
"To leave all this" said Buddy Jim
"Is cert'nly not a joke."

F
OR Buddy was going home tomorrow! Back to the city! He
just had to laugh when he thought how he had not wanted to
come to the country; and what a perfectly splendid time he
had been having all summer in spite of that. Old Bob the gardener
was such a good chum! And then there were all the Little Neighbors.
He wondered if when he got back home that he would see Reddy
Bat again. He hoped so; he wanted to thank him. He was glad he

You might also like