Skip to main content

A Django module integrating Storybook pattern library and Django

Project description

django-storybook

A Django module to intergrate the Storybook pattern library and django

Installation

    pip install django-storybook && pip install django-cors-headers

Follow the setup for django-cors-headers here.

Config

Settings.dev

Please note django_storybook disables CSRF protection to allow for local development using Docker. This should not be used in production.

    # Enable django_storybook
    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ALLOW_HEADERS = default_headers + ("Access-Control-Allow-Origin", "csrftoken")

Installed apps

Add the following to your installed apps:

INSTALLED_APPS =[
    ...
    "django_storybook",
    "corsheaders",
    ...
]

URLs

Add the following to your debug urls:

    if settings.debug:
            urlpatterns += url(r"", include(django_storybook_urls)),

Storybook

Add the following to your webpack.config.js:

        "django-storybook": "start-storybook -p 6006 & webpack --mode development --progress --watch",

While your development environment is running:

    npm run django-storybook

Creating templates

To create Storybook stories that django-storybook can interpret you need to add the following Javascript file titled "base-template.js"

    import React from 'react'

    const baseUrl = "http://0.0.0.0:8000/compile_django/"

    class BaseTemplate extends React.Component {
        constructor() {
        super()
          this.state = {
            data: null,
          };
        }

        componentDidMount() {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", baseUrl, true);
            xhr.setRequestHeader('Content-type',    'application/json')
            xhr.onload = () => {
                var template = xhr.response;
                console.log(template);
                this.setState({ template });
            }

            xhr.send(JSON.stringify( this.props ));
        }

        render() {
      if (this.state.template) {
        return  <div dangerouslySetInnerHTML={{__html:this.state.template}} />;
      }
      else {
        return <div>Loading...</div>
      }
    }
  }

  export default BaseTemplate;

Templating

To render django templates, template tags and pass in context you simply import the base template and create props that mimic the expected context. The template variable is the relative path to you desired template.

    import React from 'react';
    import BaseTemplate from '../path/to/your/base-template.js';

    // Set storybook title
    export default { title: 'Example' };

    // Create props for context
    var props = {
        template: 'path/to/your/html.html',
        context: {
            card: {
                description: "A Description",
                url: "https://wwww.williamblackie.com",
                button_title: "Vist website"
                },
            card_image: {
            url: "https://via.placeholder.com/250x150?text=William+Blackie"
            }
        },
    }


    // Export 
    export const example = () => (
        <BaseTemplate {...props}/>

    )

Deployment

Due to disabling CSRF django_storybook is not safe for production. However, deploying a static storybook using storybook deployer is a good alternative.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_storybook-0.1.4.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_storybook-0.1.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django_storybook-0.1.4.tar.gz.

File metadata

  • Download URL: django_storybook-0.1.4.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.9

File hashes

Hashes for django_storybook-0.1.4.tar.gz
Algorithm Hash digest
SHA256 78a5ab75817ec64662bc1efcbe39dfd0f76794f970f0250fb73f44850d90e5fa
MD5 d4a1e01e5e9c685016d564cb84d7696c
BLAKE2b-256 b5f0be0b7748eba38d05f98bf78ceeeeb94c57f4081b377f0bda8e91ada43128

See more details on using hashes here.

File details

Details for the file django_storybook-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_storybook-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.9

File hashes

Hashes for django_storybook-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9f4e1494a0f137575b40754436692565b7a6a6a405ce8150b5bd932aa2d87e1c
MD5 ec96ab4610cca4e4fde8338980d01c54
BLAKE2b-256 5cb5b6dcb63f4fbf656209671e2c5508be29105e5583f1e87fe30c121d7f90b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page