sagemath / sage_sample

A sample project illustrating how to write and distribute SageMath extensions
GNU General Public License v2.0
17 stars 15 forks source link
package sagemath

=================== Sage Sample Package

.. image:: https://mybinder.org/badge.svg :target: https://mybinder.org/v2/gh/sagemath/sage_sample/master

This package is designed as a simple SageMath <http://www.sagemath.org> package example to serve as a good practice reference for package developers. We follow python recommendations and adapt them to the SageMath community. You can find more advanced documentation on python package creation on How To Package Your Python Code <https://packaging.python.org/>.

This is still a work in progress. Once this example will have stabilized, the plan is to make a cookie cutter <https://cookiecutter.readthedocs.io/en/latest/>_ template out of it.

Installation

Try the demo <https://mybinder.org/v2/gh/sagemath/sage_sample/master?filepath=demo.ipynb>_ on binder ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Local install from source ^^^^^^^^^^^^^^^^^^^^^^^^^

Download the source from the git repository::

$ git clone https://github.com/sagemath/sage_sample.git

Change to the root directory and run::

$ sage -pip install --upgrade --no-index -v .

For convenience this package contains a makefile <makefile>_ with this and other often used commands. Should you wish too, you can use the shorthand::

$ make install

Install from PyPI ^^^^^^^^^^^^^^^^^^

sage_sample is distributed on PyPI. You can install it with the command::

$ sage -pip install sage_sample

Distribute via PyPI ^^^^^^^^^^^^^^^^^^^

To distribute your own package on PyPI, you will need an account on pypi.org <https://pypi.org/> (maybe at first on test.pypi.org <https://test.pypi.org/>).

You also need to install setuptools, wheel and twine::

$ pip install --upgrade setuptools wheel twine

If the README file is in Markdown format, you need to install cmarkgfm as well::

$ pip install --upgrade cmarkgfm

Then you can generate the distribution package via::

$ python setup.py sdist bdist_wheel

NB: if the package is both Python 2 and Python 3 compatible, use instead::

$ python setup.py sdist bdist_wheel --universal

Check that everything is all right::

$ twine check dist/*

Upload the package to the test PyPI repository::

$ twine upload --repository-url https://test.pypi.org/legacy/ dist/*

Check the package install in Sage::

$ sage -pip install -i https://test.pypi.org/simple sage_sample

And later, upload your distribution to the real PyPI [optionally sign it with GPG]::

$ twine upload [-s] dist/*

Usage

Once the package is installed, you can use it in Sage with::

sage: from sage_sample import answer_to_ultimate_question
sage: answer_to_ultimate_question()
42

See also the demo notebook <demo.ipynb>_.

Setup

All packaging setup is done through setup.py. To create your own package follow the strcuture of the file and change the parameters accordingly.

Source code

All source code is stored in the folder sage_sample using the same name as the package. This is not mandatory but highly recommended for clarity. All source folder must contain a __init__.py file with needed includes.

Tests

This package is configured for tests written in the documentation strings, also known as doctests. For examples, see this source file <sage_sample/ultimate_question.py>_. See also SageMath's coding conventions and best practices document <http://doc.sagemath.org/html/en/developer/coding_basics.html#writing-testable-examples>_. With additional configuration, it would be possible to include unit tests as well.

Once the package is installed, one can use the SageMath test system configured in setup.py to run the tests::

$ sage setup.py test

This is just calling sage -t with appropriate flags.

Shorthand::

$ make test

Documentation

The documentation of the package can be generated using Sage's Sphinx installation::

$ cd docs
$ sage -sh -c "make html"

Shorthand::

$ make doc

For this to work on your own package, make sure you follow the same structure as we do here:

Travis CI integration

.. image:: https://travis-ci.org/sagemath/sage_sample.svg?branch=master :target: https://travis-ci.org/sagemath/sage_sample

Scripts that run make test on various SageMath versions on the Travis CI system are included. https://docs.travis-ci.com/user/for-beginners explains how to enable automatic Travis CI builds for your GitHub-hosted project.

The scripts download and install binary releases (7.1-7.4) from a SageMath mirror. Edit .travis-install.sh if some optional or experimental SageMath packages need to be installed prior to running your package. Edit .travis.yml to change the list of SageMath versions used.

Automatically deploying documentation to GitHub pages using Travis CI