G2SageMath - Integrating Google spreadsheets with Sage math library
Integrating Google Spreadsheets to Sage
M.P. Tharindu Rusira Kumara
Department of Computer Science and Engineering,
University of Moratuwa,
Abstract - G2SageMath is a project launched with the mission
of enabling Sage to utilize Google services. In this paper, a
solution is presented that authorizes Sage to access Google
spreadsheets and load contents of a worksheet into Sage
environment. G2SageMath design considerations and possible
improvements are discussed in later sections of the paper.
Sage is a popular open source mathematics library . Sage
made its first release in early 2005 under GNU General
Public License. William Stein, a mathematician at the
University of Washington first came up with the idea of a
FOSS alternative to Magma, Maple, Mathematica and
Sage is currently available in both standalone and web
versions. G2SageMath is a third party too that can be run in
parallel with Sage standalone desktop version.
G2SageMath is a project launched in order to integrate
Google data services to Sage. G2SageMath is being
developed with two main goals. Exposing Sage to Google
services is the main objective where introducing easy data
file management is a secondary goal. As an initiative,
Google spreadsheets are made compatible with Sage
through a spreadsheet management module. From this point
onwards, the term “G2SageMath” may refer to the
spreadsheet manager module in this paper unless otherwise
In the first phase of G2SageMath, a python package was
developed that assists a user to load Google spreadsheets
directly into Sage. Sage has native python support for
loading data files. Sage uses Python's csv module to import
sage: import csv
sage: file = csv.reader(open('path-to-the-file'))
II. DESIGN AND IMPLEMENTATION
The main use case for G2SageMath is shown in Fig. 1.
Basic user requirements are described in the use case.
G2SageMath package organization and system architecture
is shown in Fig. 2 and Fig. 3 respectively. G2SageMath is
operated as a separate layer in between Google APIs and
Sage library. This is mainly because G2SageMath heavily
relies on external APIs and modularity is an essential
feature to ensure that the system is ready to accept frequent
API changes and deprecations.
At the implementation level, G2SageMath is entirely
written in Python. G2SageMath comes with all the required
library files as a self contained package so the user does not
need to install additional software to run G2SageMath. The
only requirement are a Python 2.7 interpreter and Sage itself
Fig. 1: G2SageMath use case
Fig. 2: Package organization
which are both easily available on the Internet directly from
the developers of the respective software.
G2SageMath uses gdata 2.0.17 python library and the
library files are included with the G2SageMath itself
making the whole package of code portable and ready for
quick use without any installation.
G2SageMath contains two functional modules, namely
g2sagemathlogin and g2sagemathdata.
g2sagemathlogin module has LogIn class that handles user
authentication process and the spreadsheet request
transaction. LogIn class is shown in Fig. 4.
User authentication is achieved through
gdata.docs.service.DocsService clients provided by gdata
python client library.
BadAuthentication exception, as a result of providing
invalid user name- password combination is handled by
terminating the execution with an error message.
Spreadsheet feed is retrieved with
method and the available files are listed.
Once a user specifies a spreadsheet, available worksheets
are retrieved using GetWorksheetsFeed method.
Fig. 4: g2sagemathlogin.LogIn class
Fig. 5: g2sagemathdata.DataHandler class
Fig. 3: G2SageMath architecture
Then the user is allowed to select a worksheet from any
spreadsheet available in the Drive. Parameter sskey contains
a unique key to identify the selected spreadsheet.
g2sagemathdata module contains the class DataHandler
that manages downloading the specified data file and
loading it into a Sage instance. In the Linux platform the
data file is saved to the default temporary file location /tmp.
The spreadsheet is downloaded as a csv (comma separated
value) data file.
When initializing a Sage instance G2SageMath checks
whether Sage is available in the PATH variable. If it is
found, G2SageMath calls Sage directly.
The following code checks for availability of Sage in PATH
Otherwise, G2SageMath prompts the user to assist
G2SageMath to find the Sage source packages. Tab key
sensitive path auto-completion is enabled to make
G2SageMath more user friendly.
Once G2SageMath is ready to launch a Sage instance, it
writes necessary code to init.sage file in Sage home
directory. This code is read by Sage before Sage is
Then G2SageMath executes a subprocess call to the
operating system in order to execute the initializing
command for Sage. 
G2SageMath terminates its execution with a successful
subprocess call that spawns a new Sage instance in a new
The entire work flow is managed by run.py script.
An abstract activity flow of G2SageMath is given below.
1. Run G2SageMath
2. Login to Google Drive
3. Select a spreadsheet
4. Select a worksheets
5. File is downloaded
6. If Sage is found in the system path,
1. Start Sage and load the data file to a new
7. If Sage is not found in the system path,
1. Let the user manually specify Sage path.
2. Start Sage and load the data file to a new
8. Terminate G2SageMath and hand over the
execution to Sage
G2SageMath is open for a lot of improvements. Some of the
design considerations and possible improvements are
discussed in the following lines.
In the current release of G2SageMath, default
programmaticLogin() method is used to provide user
ProgrammaticLogin() method return a authentication token
which will be used in each time a gdata.service instance
makes a request. Since the service client is not exposed
to the external environment but passes data internally, the
chances of a security breech are low. However it is possible
to introduce OAuth 2.0 authentication protocol in place of
the generic programmaticLogin() procedure.
A major drawback in OAuth 2.0 for installed applications
like G2SageMath is that, the user is required to provide a
access token generated by the authentication server at every
login instance. This code is obtained by following a instance
specific URL provided by the Google authentication server
which means the user will be required to use a web browser.
This makes the log in process cumbersome and browser
G2SageMath developed on Python 2.x. Hence, further
development consists of porting G2SageMath to Python 3.x.
Python 2 to 3 support. This task is of the minimum
difficulty as the dedicated python library lib2to3 is readily
available that will automatically convert a Python 2.x code
to the equivalent Python 3.x code with some manual
Requirement of a graphical user interface (GUI) was
considered and omitted in the design phase of G2SageMath.
The main reason was that G2SageMath is supposed to be a
support module for Sage and Sage itself is running in the
command line interface (CLI). G2SageMath is mainly
targeted to run with Sage command line interface even
though the notebook version can be run on a browser.
Possible measures were taken to provide the G2SageMath
user with an uniform experience in both G2SageMath and
Sage terminals. (Eg. Observe Fig 6 and Fig 7)
G2SageMath runs as an independent package which calls
Sage upon its successful completion. One possible idea of
modification is that implementing G2SageMath as an
internal command in Sage that can be called while Sage is
running. Sage can run python scripts directly through its
terminal because Sage is shipped with an embedded Python
interpreter. This Python interpreter is typically available in
SAGE_HOME/local/bin. But this Python interpreter fails to
find third party system dependencies like external libraries.
So this prevents us from calling G2SageMath from the Sage
terminal. So in order to allow G2SageMath to run as an
internal Sage command, some alterations in the Sage
packages are needed. to be carried out.
G2SageMath v 1.0, first release of G2SageMath addresses
requirements specified in the use case in Fig 1.The basic
operation of G2SageMath v 1.0 is to work as a supporting
module for Sage that links Sage to Google Drive. Many
improvements can be introduced as discussed in the
previous section. The aim of G2SageMath is to introduce
Sage math library to utilize Google cloud services and
G2SageMath v 1.0 enables Sage to access Google Drive.
G2SageMath v 1.0 is available at Github
distributed under the terms of General Public License. 
Being an open source initiative, G2SageMath welcomes
interested developers to extend G2SageMath.
 Sage official website, [online] Available:www.sagemath.org
 gdata-service, gdata-python documentation, [online]
 Using OAuth 2.0 for installed applications, Google
 2to3 - Automated Python 2 to 3 code translation, Official
Python documentation, [online]
 17.1. subprocess — Subprocess management , Python
 General Public License, [online] Available:
Fig 6: G2SageMath welcome screen in Linux terminal
Fig 7: Sage welcome screen in Linux terminal