Giter VIP home page Giter VIP logo

sage_sample's Introduction

Sage Sample Package

image

This package is designed as a simple SageMath 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.

This is still a work in progress. Once this example will have stabilized, the plan is to make a cookie cutter template out of it.

Installation

Try the demo 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 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 (maybe at first on 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.

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. See also SageMath's coding conventions and best practices document. 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:

  • Create a docs folder containing the exact same Makefile and a source folder.
  • Copy and paste the docs/source/conf.py file from this package and update the few project specific variables at the beginning of the file.
  • Create an index.rst file as well as a <module name>.rst file for each module you want on the documentation.

Travis CI integration

image

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

  • First do the steps described above to enable Travis CI integration of your GitHub-hosted project.
  • If you don't already have GitHub pages for your project: Create and checkout a branch gh-pages in your repository and put an empty file .nojekyll in it (see https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/). Then commit it and push it to GitHub:

    $ git clone --single-branch --depth 1 https://github.com/USER/PROJECT.git gh-pages
    $ cd gh-pages
    $ git checkout --orphan gh-pages
    $ git rm -rf .
    $ touch .nojekyll
    $ git add .nojekyll
    $ git commit -m "Initial commit"
    $ git push -u origin gh-pages
    $ cd ..
  • (Back in your working copy:) Generate a new ssh key pair with an empty passphrase:

    $ ssh-keygen -t dsa -f .travis_ci_gh_pages_deploy_key
  • Add the public ssh key (contents of the file .travis_ci_gh_pages_deploy_key.pub) to your GitHub repository as a deploy key (Settings/Deploy keys/Add deploy key). Title: Key for deploying documentation to GitHub pages. Check Allow write access.
  • Install the Travis CI command-line client from https://github.com/travis-ci/travis.rb:

    $ gem install travis
  • Log in to Travis CI using your GitHub credentials:

    $ travis login
  • Encrypt the private ssh key, add the decryption keys as secure environment variables to Travis CI, and add code to .travis.yml to decrypt it:

    $ travis encrypt-file .travis_ci_gh_pages_deploy_key --add before_script
  • Add the encrypted private ssh key to the repository:

    $ git add .travis_ci_gh_pages_deploy_key.enc
  • Have git ignore the other keys (and the gh-pages directory):

    $ echo >> .gitignore
    $ echo "/.travis_ci_gh_pages_deploy_key" >> .gitignore
    $ echo "/.travis_ci_gh_pages_deploy_key.pub" >> .gitignore
    $ echo "/gh-pages" >> .gitignore
    $ git add .gitignore
  • Optionally, edit .travis.yml to adjust variables DEPLOY_DOC_...
  • Commit all changes to GitHub. The Travis CI build should then run automatically and deploy it:

    $ git add .travis.yml
    $ git commit -m "Deploy built documentation to GitHub"
    $ git push
  • The deployed documentation will be available at: https://USER.github.io/PROJECT/ This can be customized by changing DEPLOY_DOC_TO_DIRECTORY=/ to another directory in .travis.yml For example, setting DEPLOY_DOC_TO_DIRECTORY=doc/html will make the deployed documentation available at: https://USER.github.io/PROJECT/doc/html/

sage_sample's People

Contributors

ablondin avatar egourgoulhon avatar koffie avatar mkoeppe avatar nthiery avatar seblabbe avatar vivianepons avatar williamstein avatar zerline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sage_sample's Issues

Add support for readthedocs

https://readthedocs.org/ can be a nice way for sage packages to make their documentation available. So it would be nice to investigate how to showcase how to do this in sage_sample.

A first attempt is here: https://readthedocs.org/projects/sage-sample/

As expected, the difficulty is that compiling the documentation with Sphinx requires importing the Sage library. So we need to figure out a way to specify that Sage is a dependency (see readthedocs's build process). At this stage, we can't do it in setup.py.

There also are some ressource limitations; but as long as we do a binary install of Sage, that should not be a problem (unless they have also a disk space limitation).

cc: @embray

make doc fails, does not find sage_autodoc

Again, on Sage debian package (sagemath:amd64 8.6-5)

(sage-sh) $ make doc
cd docs && sage -sh -c "make html"
make[1] : on entre dans le répertoire « /home/odile/prj/sage_sample/docs »
sphinx-build -b html -d build/doctrees   source build/html
Running Sphinx v1.8.3

Extension error:
Could not import extension sage_autodoc (exception: No module named sage_autodoc)

sage -pip not working on debian

Testing sage_sample installation on debian, with the debian package (sagemath:amd64 8.6-5 )

$ sage -sh
(sage-sh) odile@cecile:sage_sample$ sage -pip install --upgrade --no-index -v .
sage-run received unknown option: -pip
usage: sage [options]
Try 'sage -h' for more information.

So I had to do:

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

(which btw is not really documented)

sdist not working properly

Most of the files created by the sdist command should actually not be there

$ sage -python setup.py sdist
/opt/sage/local/lib/python2.7/site-packages/setuptools/dist.py:333: UserWarning: Normalizing '0.1.0
' to '0.1.0'
  normalized_version,
running sdist
running egg_info
writing sage_sample.egg-info/PKG-INFO
writing top-level names to sage_sample.egg-info/top_level.txt
writing dependency_links to sage_sample.egg-info/dependency_links.txt
writing manifest file 'sage_sample.egg-info/SOURCES.txt'
running check
creating sage_sample-0.1.0
creating sage_sample-0.1.0/docs
creating sage_sample-0.1.0/docs/source
creating sage_sample-0.1.0/sage_sample
creating sage_sample-0.1.0/sage_sample.egg-info
copying files to sage_sample-0.1.0...
copying .gitignore -> sage_sample-0.1.0
copying .travis-deploy-doc.sh -> sage_sample-0.1.0
copying .travis-install.sh -> sage_sample-0.1.0
copying .travis-test.sh -> sage_sample-0.1.0
copying .travis.yml -> sage_sample-0.1.0
copying .travis_ci_gh_pages_deploy_key.enc -> sage_sample-0.1.0
copying LICENSE -> sage_sample-0.1.0
copying README -> sage_sample-0.1.0
copying README.rst -> sage_sample-0.1.0
copying VERSION -> sage_sample-0.1.0
copying makefile -> sage_sample-0.1.0
copying setup.py -> sage_sample-0.1.0
copying docs/Makefile -> sage_sample-0.1.0/docs
copying docs/source/conf.py -> sage_sample-0.1.0/docs/source
copying docs/source/index.rst -> sage_sample-0.1.0/docs/source
copying docs/source/ultimate_question.rst -> sage_sample-0.1.0/docs/source
copying sage_sample/__init__.py -> sage_sample-0.1.0/sage_sample
copying sage_sample/ultimate_question.py -> sage_sample-0.1.0/sage_sample
copying sage_sample.egg-info/PKG-INFO -> sage_sample-0.1.0/sage_sample.egg-info
copying sage_sample.egg-info/SOURCES.txt -> sage_sample-0.1.0/sage_sample.egg-info
copying sage_sample.egg-info/dependency_links.txt -> sage_sample-0.1.0/sage_sample.egg-info
copying sage_sample.egg-info/top_level.txt -> sage_sample-0.1.0/sage_sample.egg-info
Writing sage_sample-0.1.0/setup.cfg
Creating tar archive
removing 'sage_sample-0.1.0' (and everything under it)

The issue can be fixed by dropping setuptools in favor of distutils.

Example of packaging an executable

This sample seems to focus on packaging pure Python code. I mainly used Sage packaging to package some programs written in different languages, that were then compiled and copied to $SAGE_LOCAL/bin. I don't see how this can be achieved with the technique explained in this sample.

sage-package not found

$ sage -python2 -m pip install --upgrade --no-index -v .
...
Running command python setup.py egg_info
...
DistributionNotFound: No matching distribution found for sage-package (from sage-sample==0.3.0)

remove all the $ signs from the documentation.

Currently there are a lot of commands mentioned in the documentation that the user should execute. And they are all prefixed by $ signs. This is annoying since it hinders easy copy pasting.
For example try executing this

$ mkdir mydir
$ cd mydir
$ echo "hello world" > some_file
$ cat some_file
$ cd ..
$ rm -r mydir

v.s executing this:

mkdir mydir
cd mydir
echo "hello world" > some_file
cat some_file
cd ..
rm -r mydir

Turn this package template into a cookie for CookieCutter

CookieCutter is a wizard-style command line utility that lets users initialize new projects from project templates, after asking a couple questions.

It would be great to have a project template for Sage projects, supporting various use cases:

  • Trivial project with a single .sage file
  • Project with a couple .py files and demo notebooks
  • Full scale library

Julia has something like this and @defeo mentioned it was pretty effective to encourage people to share and publish their code by making the process darn easy.

Using CookieCutter should also make it easy to support optional features (like Travis Support; see discussion on #4).

(thanks @defeo for bringing this up and @embray for suggesting CookieCutter).

Documentation deploys on master branch failing

Hi, I think there is something wrong with the encrypted key / deploy key of this repo. I fixed all problems in my own fork. But I cannot do that in this repo since, even though I have write acces, I don't have the right to add a new deploy key. I guess you need to be admin for that.

I am willing to do the needed key generation and encryption and commit it to the correct branches. Is there anyone where I can send the public key to so that it can be added as a deploy key?

Support for compiling the doc with and without Sage

Requires:

  • Figuring a way to mock sage modules so that the Python code can be loaded into Python for Sphinx's instrospection
  • Making it possible to use, or not, mathjax and theme files provided by Sage

This would enable trivial ReadTheDocs integration #6; but maybe it's not worth the effort and one should take the alternative route described there.

docs: _static subdirectory not present

When you "make html" within the ./docs subdirectory, you get a warning:

--%<
copying static files... WARNING: html_static_path entry u'/home/odile/odk/sage/git/sage_sample/docs/source/_static' does not exist
--%<

This warning would disappear if you change line #167 in file ./docs/source/conf.py

#html_static_path = ['_static']
html_static_path = []

As CSS file is not actually required ..

binder support

binder is a neat web service that gives interactive access to the Jupyter notebooks stored in a GitHub repository. It would be neat to showcase how to include demo notebooks in the package that users can immediately play with on mybinder.

Prerequisite: sagemath/docker-images#23

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.