fresco-static


Namefresco-static JSON
Version 0.5 PyPI version JSON
download
home_pageNone
SummaryStatic file handling for fresco
upload_time2024-10-16 12:03:19
maintainerNone
docs_urlNone
authorOliver Cope
requires_pythonNone
licenseApache
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            fresco-static - static file serving for fresco
==============================================


Basic usage
-----------

Installation::

    pip install fresco-static

    # If you want brotli compression enabled
    pip install fresco-static[brotli]

Simple configuration::

    from fresco import FrescoApp
    from fresco_static import serve_directory, serve_file

    app = FrescoApp()

    # Serve a directory
    app.route_all("/static", ALL=serve_directory("path/to/static/files"))

    # Serve a single file
    app.route("/favicon.ico"", ALL=serve_file("path/to/favicon.ico"))

    # Serve a directory that's part of a Python package
    app.route_all(
        "/static",
        ALL=serve_package_directory("mypackage", "static/files")
    )


Advanced options::

    app = FrescoApp()
    app.route_all(
        "/static",
        ALL=serve_directory(
            "path/to/static/files",

            # Specify custom headers. These will be set for all files served.
            headers={"cache-control": "max-age 86400"},

            # Compression algorithms to offer, in order of preference
            compression="brotli,gzip"

            # Directory to cache compressed copies of files. The same directory may
            # safely be shared between instances
            cachedir="/tmp/staticfilecache"
        )
    )


App-wide usage
--------------

These instructions are for creating a single ``StaticFiles`` object that serves
static requests across the entire application.

Create a fresco app::

    app = FrescoApp()

Let's assume this lives in an application that's structured like this::

    mypackage
    ├── __init__.rst
    ├── app.py
    ├── static
    │   └── photo.jpg
    └── setup.py


Once you have the app, you can create an instance of ``StaticFiles``:

.. code-block:: python

    static = StaticFiles(app)

StaticFiles automatically adds routes for the path ``/static``.
Static files will be available under ``/static/<packagename>/<path>``
eg ``/static/mypackage/photo.jpg``. You can change these defaults:

.. code-block:: python

    static = StaticFiles(app,
                         prefix='/media',
                         route_name='media',
                         cache_max_age=3600)

You can have multiple packages serving files through a single StaticFiles
object without fear of conflict.

Now you can start configuring static source directories:

.. code-block:: python

    # Mount directory '/www/mysite/htdocs'. The first argument is an arbitrary
    # name used to identify this source. You can use whatever string you like.
    static.add_directory('site-htdocs', '/site/htdocs', cache_max_age=60)

    # Serve files located in a 'subdir' directory within the python package
    # 'mypackage'
    static.add_package('mypackage', 'subdir', cache_max_age=86400)

The ``cache_max_age`` argument specifies for how long (in seconds)
browsers and proxies can cache responses.
For development you might want to set this to zero,
but in production use you should
set this to a reasonable value and
configure a caching HTTP proxy server.
When adding source directories you can omit this argument, and the default
(configured when you created the ``StaticFiles`` object)
will be used instead.

``static.pathfor`` generates URLs for static content.
You will probably want to include this
in your templating system's default namespace. How you do that depends on how
you've integrated the templating system, but it would typically be something
like this:

.. code-block:: python

    templating.contextprocessor({'static': static.pathfor})

Call this in templates to link to static files, eg:

.. code-block:: html

    <!-- Reference a file from the "site-htdocs" source
    -->
    <img src="{{ static('site-htdocs/photo.jpg') }}" alt="My photo" />

    <!-- Reference a file from the "mypackage" source
    -->
    <img src="{{ static('mypackage/photo.jpg') }}" alt="My photo" />

    <!-- Path doesn't begin with a source name — all sources will be
         searched for a matching file
    -->
    <img src="{{ static('cat-pictures/miaow.gif') }}" alt="My photo" />


0.5 (released 2024-10-16)
-------------------------

* Added serve_package_directory function
* Improved support for HEAD requests

0.4 (released 2024-07-11)
-------------------------

* Added view-based integration

0.3 (released 2024-05-02)
-------------------------

* Added Python 3.12 support
* Dropped support for Python 3.8 and older

0.2 (released 2018-08-01)
-------------------------

* Source names are no longer required to be python identifiers


0.1
----

* Initial release

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fresco-static",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Oliver Cope",
    "author_email": "oliver@redgecko.org",
    "download_url": null,
    "platform": null,
    "description": "fresco-static - static file serving for fresco\n==============================================\n\n\nBasic usage\n-----------\n\nInstallation::\n\n    pip install fresco-static\n\n    # If you want brotli compression enabled\n    pip install fresco-static[brotli]\n\nSimple configuration::\n\n    from fresco import FrescoApp\n    from fresco_static import serve_directory, serve_file\n\n    app = FrescoApp()\n\n    # Serve a directory\n    app.route_all(\"/static\", ALL=serve_directory(\"path/to/static/files\"))\n\n    # Serve a single file\n    app.route(\"/favicon.ico\"\", ALL=serve_file(\"path/to/favicon.ico\"))\n\n    # Serve a directory that's part of a Python package\n    app.route_all(\n        \"/static\",\n        ALL=serve_package_directory(\"mypackage\", \"static/files\")\n    )\n\n\nAdvanced options::\n\n    app = FrescoApp()\n    app.route_all(\n        \"/static\",\n        ALL=serve_directory(\n            \"path/to/static/files\",\n\n            # Specify custom headers. These will be set for all files served.\n            headers={\"cache-control\": \"max-age 86400\"},\n\n            # Compression algorithms to offer, in order of preference\n            compression=\"brotli,gzip\"\n\n            # Directory to cache compressed copies of files. The same directory may\n            # safely be shared between instances\n            cachedir=\"/tmp/staticfilecache\"\n        )\n    )\n\n\nApp-wide usage\n--------------\n\nThese instructions are for creating a single ``StaticFiles`` object that serves\nstatic requests across the entire application.\n\nCreate a fresco app::\n\n    app = FrescoApp()\n\nLet's assume this lives in an application that's structured like this::\n\n    mypackage\n    \u251c\u2500\u2500 __init__.rst\n    \u251c\u2500\u2500 app.py\n    \u251c\u2500\u2500 static\n    \u2502\u00a0\u00a0 \u2514\u2500\u2500 photo.jpg\n    \u2514\u2500\u2500 setup.py\n\n\nOnce you have the app, you can create an instance of ``StaticFiles``:\n\n.. code-block:: python\n\n    static = StaticFiles(app)\n\nStaticFiles automatically adds routes for the path ``/static``.\nStatic files will be available under ``/static/<packagename>/<path>``\neg ``/static/mypackage/photo.jpg``. You can change these defaults:\n\n.. code-block:: python\n\n    static = StaticFiles(app,\n                         prefix='/media',\n                         route_name='media',\n                         cache_max_age=3600)\n\nYou can have multiple packages serving files through a single StaticFiles\nobject without fear of conflict.\n\nNow you can start configuring static source directories:\n\n.. code-block:: python\n\n    # Mount directory '/www/mysite/htdocs'. The first argument is an arbitrary\n    # name used to identify this source. You can use whatever string you like.\n    static.add_directory('site-htdocs', '/site/htdocs', cache_max_age=60)\n\n    # Serve files located in a 'subdir' directory within the python package\n    # 'mypackage'\n    static.add_package('mypackage', 'subdir', cache_max_age=86400)\n\nThe ``cache_max_age`` argument specifies for how long (in seconds)\nbrowsers and proxies can cache responses.\nFor development you might want to set this to zero,\nbut in production use you should\nset this to a reasonable value and\nconfigure a caching HTTP proxy server.\nWhen adding source directories you can omit this argument, and the default\n(configured when you created the ``StaticFiles`` object)\nwill be used instead.\n\n``static.pathfor`` generates URLs for static content.\nYou will probably want to include this\nin your templating system's default namespace. How you do that depends on how\nyou've integrated the templating system, but it would typically be something\nlike this:\n\n.. code-block:: python\n\n    templating.contextprocessor({'static': static.pathfor})\n\nCall this in templates to link to static files, eg:\n\n.. code-block:: html\n\n    <!-- Reference a file from the \"site-htdocs\" source\n    -->\n    <img src=\"{{ static('site-htdocs/photo.jpg') }}\" alt=\"My photo\" />\n\n    <!-- Reference a file from the \"mypackage\" source\n    -->\n    <img src=\"{{ static('mypackage/photo.jpg') }}\" alt=\"My photo\" />\n\n    <!-- Path doesn't begin with a source name \u2014 all sources will be\n         searched for a matching file\n    -->\n    <img src=\"{{ static('cat-pictures/miaow.gif') }}\" alt=\"My photo\" />\n\n\n0.5 (released 2024-10-16)\n-------------------------\n\n* Added serve_package_directory function\n* Improved support for HEAD requests\n\n0.4 (released 2024-07-11)\n-------------------------\n\n* Added view-based integration\n\n0.3 (released 2024-05-02)\n-------------------------\n\n* Added Python 3.12 support\n* Dropped support for Python 3.8 and older\n\n0.2 (released 2018-08-01)\n-------------------------\n\n* Source names are no longer required to be python identifiers\n\n\n0.1\n----\n\n* Initial release\n",
    "bugtrack_url": null,
    "license": "Apache",
    "summary": "Static file handling for fresco",
    "version": "0.5",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8544840379727eff22f55d6ee0de254ee950adc44cb2e82892ca176251597c98",
                "md5": "72692d61ee993c6e3109955c23bf3afc",
                "sha256": "475395e8be8b3d4e311a8cd9c69c5f6e26bc8b03b9c23d17ae4f8de1e32eaf79"
            },
            "downloads": -1,
            "filename": "fresco_static-0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "72692d61ee993c6e3109955c23bf3afc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 14157,
            "upload_time": "2024-10-16T12:03:19",
            "upload_time_iso_8601": "2024-10-16T12:03:19.354872Z",
            "url": "https://files.pythonhosted.org/packages/85/44/840379727eff22f55d6ee0de254ee950adc44cb2e82892ca176251597c98/fresco_static-0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-16 12:03:19",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "fresco-static"
}
        
Elapsed time: 0.88071s