Posts Tagged ‘api’

Document your Django project using Sphinx documentation tool and reStructuredText

Wednesday, July 28th, 2010

So you wrote a cool documentation in your Python code using docstring and reStructuredText, in order to let other people understand your API. What's the next step? To export it in html/pdf etc.

I wrote an XML-RPC service providing Dynamic Ridesharing functionalities for my Bachelor Computer Science thesis. It was written using Python and Django. Obviously, I need to document my XML-RPC methods for people that will write clients, therefore an API is needed.

Many tools for exporting Python documentation exist, but the most comfortable one for Django seems to be Sphinx. As I said, it seems. Because none of those tools are explicitly written for Django projects. There are some posts around that seem to teach you how to use Sphinx for document your Python Django project and therefore export API. However, I feel stupid as I could not fully understand any of them. So here I am writing a tiny how-to for exporting the documentation of a Django project using Sphinx, docstring and reStructuredText.

Note that this is neither a reStructuredText nor a Sphinx tutorial, as their respective websites are clear enough. I suppose you already have Sphinx installed and your code is already documented. Also, this is for Gnu/Linux and Mac Os X (should work on *BDS too).

Ok, let's imagine that we have a Django project with the following (not very common) skeleton:

   dycapo
   |-settings.py
   |-urls.py
   |-templates
   |-server
   |---driver.py
   |---passenger.py
   |---common.py
   |---models
   |------location.py
   |------mode.py
   |------person.py
   |------prefs.py
   |------trip.py
   |------reponse.py

Where:

  • dycapo is the project name/folder
  • server is an application of the project
  • driver.py, passenger.py, common.py are the modules holding the functions we want to document
  • models contains Django's splitted models for server application

Yeah, the example is taken from my project. I'm using it because it has some non common things such as functions defined outside views.py and splitted models.

Open the terminal. First, we set some environment variables:

export PYTHONPATH=$PYTHONPATH:/home/bodom_lx/Projects:/home/bodom_lx/Projects/dycapo
export DJANGO_SETTINGS_MODULE=dycapo.settings

From now on, don't close the terminal or you will loose the below defined variables.

Be sure to change the values to fit your needs. Then, following Sphinx quick start tutorial, go to your project folder (it would be /home/bodom_lx/Projects/dycapo for me) and use

sphinx-quickstart

Like the tutorial says, answer the questions and be sure to say yes to the “autodoc” extension. Sphinx adds three new directories and three new files to your project's root:

   dycapo
   |-settings.py
   |-urls.py
   |-templates
   |-_build
   |-_static
   |-_templates
   |-Makefile
   |-conf.py
   |-index.rst
   |-server
   |---driver.py
   |---passenger.py
   |---common.py
   |---models
   |------location.py
   |------mode.py
   |------person.py
   |------prefs.py
   |------trip.py
   |------reponse.py

If we generate the documentation right now, using

make html

Something will be created in directory _build/html. Here is a screenshot:

Sphinx Default Generated Documentation

Impressive. But completely empty. Module index link is broken, nothing else works. Ok, let's add something to our documentation!
Sphinx master file has been generated by sphinx-quickstart command. It is our index.rst file. Let's take a look at it:

.. Dycapo Server documentation master file, created by
   sphinx-quickstart on Tue Jul 27 17:46:52 2010.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.
 
Welcome to Dycapo Server's documentation!
=========================================
 
Contents:
 
.. toctree::
   :maxdepth: 2
 
Indices and tables
==================
 
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

Do you notice the correlation between this file and the screenshot?

We will now add the documentation for the functions contained in server/.
Create a new file in the root of your project, called driver.rst. We will now add the code for documenting server/driver.py

Driver XML-RPC methods
======================
 
.. automodule:: server.driver
	:members:

driver.rst must be referenced from index.rst in some way! This is how we do it: modify index.rst:

.. Dycapo Server documentation master file, created by
   sphinx-quickstart on Tue Jul 27 17:46:52 2010.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.
 
Welcome to Dycapo Server's documentation!
=========================================
 
Contents:
 
.. toctree::
   :maxdepth: 2
 
   driver
 
Indices and tables
==================
 
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

Running again a make html produces the following output:

Sphinx new Index

Sphinx Documentation for driver module

Not bad for having written quite nothing! The same can be achieved for server/common.py, server/passenger.py and server/models by creating the corresponding .rst files and then including them in index.rst under driver.

Final result:

Sphinx Final Documentation Index

Now you should have the basis for documenting your whole project. Browse Sphinx documentation for more information and options.

References
Below are the posts I found when searching for how to export documentation for a Django project. My post also takes something from all of them.

Back to Dycapo Server

Thursday, July 22nd, 2010

I've finally gotten back to Dycapo development. During the Summer I will write my Bachelor thesis in Computer Science about the project.

Therefore, in order to write the thesis, I will first write a complete set of API and the Protocol as well. My colleague Riccardo will also report bugs of the Server and I will fix them!

I hope to be able to release a v1.0.0 version before October!

Dycapo Road to v0.5.0 – let’s look for safety and performance

Saturday, May 29th, 2010

The next version of Dycapo Server is scheduled between 2010-06-06 and 2010-06-13. Almost all prototype methods have been implemented in Dycapo v0.4.0. Therefore, the next release will probably be the last prototype of Dycapo Server, on which I will write my Bachelor Thesis this Summer, thus defining first complete versions of Dycapo Protocol and Server API.
Dycapo v0.5.0 will be a release in which I will try to add some safety/privacy triggers and enhance performance.

I hope I will have the possibility to build a real public system beginning from October. This will depend whether I find some sponsors and if I will stay working at FBK.

Announcing Dycapo Server v0.4.0, first complete Prototype

Saturday, May 22nd, 2010

I'm glad to announce the release of Dycapo Server v0.4.0. In this release I completed the set of possible operations that users can do, except the possibility to register to the System. All the methods are working, according to tests. I did not implemented security/privacy checks yet, this is planned for version v0.5.0.

You can download Dycapo Server from the download page at dycapo.org.

The following are the release notes for Dycapo Server v0.4.0.

RELEASE NOTES
***************

2010-05-22  Daniel Graziotin  

Dycapo v0.4.0 is the fourth prototype version on which we are building our APIs.

Dycapo v0.4.0 incorporates and shows:
* Adoption of Dycapo Protocol [http://dycapo.org/Protocol]
* The introduction of API [http://dycapo.org/Server/API]
* Integration of Dycapo models with Django models
* Authentication system
* A a Geo-location based search Algorithm
* Geo-location methods for Persons
* Insertion of a trip by a driver
* Start of a trip by a driver
* Search of a trip by a rider
* Send a ride request to a driver
* Let the driver accept the ride request
* Let the rider inform the system that the driver picked him/she
* Let the rider inform the system that the he/she arrived to destination
* Let the driver finish a Trip
* A full set of tests
CHANGES SINCE v0.3.0
***************

tests/
* All new XML-RPC methods covered

tests/test_multiple_matching.py
* methods to check the geolocation based algorithm of Dycapo

server/utils.py
* Bugs fixed

server/rider.py
* Added start_ride(trip) method, that informs the system that a Ride has
been started
* Added finish_ride(trip) method, that informs the system that a Ride
has been finished

server/models/prefs.py
* Overridden save() method that checks for duplicates in the Database and
saves space

server/models/mode.py
* Added a reference to Person for checking for duplicates in the Database and
saving space

server/models/location.py
* Added factory method get_location_from_geopy_point(point) that given a Geopy
Point, it returns the corresponding Dycapo Location object
* Added get_box_around(diagonal_meters) method that given a diagonal, it returns
a GeoRSS box around the location
* Added complete_fields() method that acts as a wrapper to the methods for
completing missing attributes of a Location, using geocoding

server/matching.py
* Enhanced get_trips_destination_near_location(destination) that now uses
Location.get_box_around() to compute a dynamic delta of latitude and longitude
* Enhanced get_trips_destination_near_location(destination) that also looks for
trips with empty seats

server/driver.py
* Enhanced add_trip() method that now re-uses Mode objects if already present
in the Database instead of creating a new one each time
* Fixed bugs in check_ride_requests(trip) method

Dycapo, road to v0.2.0

Thursday, April 15th, 2010

I am preparing a new release of Dycapo Server. The new version will include some bug fixes but many improvements.

For now on, I am working on

  • Adding new fields to Participation objects, for improving safety of people
  • Adding RPC methods regarding the current position of people
  • Improving the returns of Trips

The new release, that is scheduled around the end of the next week, will also have a major update in the documentation. I would like to improve the Protocol section to add real tables of the exchanged objects. Also the first APIs will be written.

Dycapo Project has a new Home!

Monday, April 5th, 2010

Today I set up a brand new project WebSite for Dycapo! The site has permanently moved to http://dycapo.org. Please refer to this one from now on.

In the next weeks, we will simplify our entities and begin to write the first API draft.