summaryrefslogtreecommitdiff
path: root/docs/source/using.rst
blob: e7e400c6b902a4fe36d846d5d51470629d9d53e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Using XStatic
=============

The XStatic package does only offer the most fundamental functions for
dealing with static files (and this is very much the point of XStatic:
being low-fat).

The only bit of code is in XStatic.main, class XStatic.

When you instantiate an object of this class, it'll read the uppercase
attributes from the xstatic module you give to it and make them available
as lowercase instance attributes.

E.g. (we use the xstatic-jquery package as example, see also the code
example below):

* xstatic.pkg.jquery.NAME -> xs.name
* xstatic.pkg.jquery.BASE_DIR -> xs.base_dir

Thus, you have all the metadata that came with the xstatic-jquery package
easily available.

Example code to setup local file serving
----------------------------------------

::

    from xstatic.main import XStatic
    # names below must be package names
    mod_names = [
        'jquery', 'bootstrap', 'font_awesome',
    ]
    pkg = __import__('xstatic.pkg', fromlist=mod_names)
    serve_files = {}
    for mod_name in mod_names:
        mod = getattr(pkg, mod_name)
        xs = XStatic(mod, root_url='/static', provider='local', protocol='http')
        serve_files[xs.name] = xs.base_dir

    # now, serve_files has the mapping name -> base_dir for all the xstatic
    # packages you want to use. you can use it in your python code to set
    # up the static file serving.


In this example, we wanted to use the local static files we got within the
xstatic-* packages.

For some packages there is also a CDN available, you can use it by giving the
appropriate provider (not 'local') and protocol (see the xstatic-* package metadata about which cdnnames and protocols are available for the package):

::
    xs = XStatic(mod, provider='cdnname', protocol='https')
    print xs.base_url

Note: base_url is often a str (as you maybe have expected). But it also can
be a dict (which maps relative pathes to full urls) - we needed that for some
CDNs where one can not just compute the full url from base url + relative path.

The Xstatic class also has a simple url_for(relative_path) method which
computes the full url - for local URLs as well as for CDN URLs.