Changes between Version 5 and Version 6 of docs_plotly


Ignore:
Timestamp:
May 7, 2018, 8:30:01 AM (2 years ago)
Author:
ehb
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • docs_plotly

    v5 v6  
    162162 * responsive resizing is not implemented, but can be added upon request
    163163
    164  
     164== python example ==
     165{{{
     166#!/usr/bin/python
     167import json, sys, StringIO
     168
     169if __name__=='__main__':
     170
     171# get JSON input object / not used in this example
     172        argv_io_string = StringIO.StringIO(sys.argv[1])
     173        json_variables = json.load(argv_io_string)
     174
     175# setup basic output object
     176
     177        output = {}
     178
     179# prepare a plot
     180
     181        barchart_json = '''
     182{
     183    "data": [
     184        {
     185            "x": [
     186                "giraffes",
     187                "orangutans",
     188                "monkeys"
     189            ],
     190            "y": [
     191                20,
     192                14,
     193                23
     194            ],
     195            "type": "bar"
     196        }
     197    ]
     198}
     199'''.strip()
     200
     201# convert to dict
     202        barchart = json.loads(barchart_json)
     203
     204        lineplot_json = '''
     205{
     206    "data" : [
     207        {
     208          "x": [1, 2, 3, 4],
     209          "y": [10, 15, 13, 17],
     210          "mode": "markers",
     211          "marker": {
     212            "color": "rgb(219, 64, 82)",
     213            "size": 12
     214          }
     215        },
     216        {
     217          "x" : [2, 3, 4, 5],
     218          "y" : [16, 5, 11, 9],
     219          "mode" : "lines",
     220          "line" : {
     221            "color" : "rgb(55, 128, 191)",
     222            "width": 3
     223          }
     224        },
     225        {
     226          "x" : [1, 2, 3, 4],
     227          "y" : [12, 9, 15, 12],
     228          "mode" : "lines+markers",
     229          "marker" : {
     230            "color" : "rgb(128, 0, 128)",
     231            "size": 8
     232          },
     233                "line" : {
     234            "color" : "rgb(128, 0, 128)",
     235            "width" : 1
     236            }
     237        }
     238        ],
     239    "layout" : {
     240        "title" : "Line and Scatter Styling"
     241    }
     242}
     243'''.strip()
     244
     245        lineplot = json.loads(lineplot_json)
     246
     247# you can also work with this dictionary directly
     248        lineplot['data'][0]['y'] = [20, 10, 15, 23]
     249
     250# a combination of using the json format as above and then setting the variable values afterwards
     251#  (e.g. during computation) is a reasonable pattern to use
     252
     253# populate output object
     254
     255        output['barchart'] = barchart;
     256        output['lineplot'] = lineplot;
     257
     258# output the final json object
     259# N.B. make sure you do not output anything else to standard output (i.e. via print or other)
     260#      that will be attempted to be read as JSON and you will get an error
     261
     262        print json.dumps(output)
     263}}}
     264