Skip to content

Instantly share code, notes, and snippets.

@kikocorreoso
Created November 22, 2014 11:11
Show Gist options
  • Save kikocorreoso/a400e60e8005669ab2ec to your computer and use it in GitHub Desktop.
Save kikocorreoso/a400e60e8005669ab2ec to your computer and use it in GitHub Desktop.
Example of use og HighStock with Bython and Brythonmagic in the IPython notebook
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:59e08345814a793076356461651cbe57514230d2c24e8d378a20ffaf500f168b"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext brythonmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Load Brython library"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%html\n",
"<script type=\"text/javascript\" src=\"http://www.brython.info/src/brython_dist.js\"></script>"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<script type=\"text/javascript\" src=\"http://www.brython.info/src/brython_dist.js\"></script>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML object>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Load Highstock library"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%HTML\n",
"<script type=\"text/javascript\" src=\"//cdnjs.cloudflare.com/ajax/libs/highstock/2.0.4/adapters/standalone-framework.js\"></script>\n",
"<script type=\"text/javascript\" src=\"http://code.highcharts.com/stock/highstock.js\"></script>"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<script type=\"text/javascript\" src=\"//cdnjs.cloudflare.com/ajax/libs/highstock/2.0.4/adapters/standalone-framework.js\"></script>\n",
"<script type=\"text/javascript\" src=\"http://code.highcharts.com/stock/highstock.js\"></script>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML object>"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Attempt at making StockChart"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"html = \"\"\"<div id=\"hc_test\" style=\"width: 700px; height: 300px;\"></div>\"\"\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%brython -h html\n",
"from javascript import JSConstructor\n",
"from browser import window\n",
"from datetime import datetime\n",
"\n",
"Highcharts = window.Highcharts\n",
"\n",
"#This doesn't work.\n",
"hc = JSConstructor(Highcharts.StockChart)\n",
"#hc = JSConstructor(Highcharts.Chart)\n",
"\n",
"config = {\n",
" 'chart': {\n",
" 'renderTo': 'hc_test'\n",
" },\n",
" 'rangeSelector': {\n",
" 'selected' : 1\n",
" },\n",
" 'title' : {\n",
" 'text': 'USD to EUR exchange rate'\n",
" },\n",
" 'tooltip': {\n",
" 'style': {\n",
" 'width': '200px'\n",
" },\n",
" 'valueDecimals': 4\n",
" },\n",
"\n",
" 'yAxis': {\n",
" 'title': {\n",
" 'text': 'Exchange rate'\n",
" }\n",
" },\n",
" 'series': [{\n",
" 'name' : 'USD to EUR',\n",
" 'data' : [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],\n",
" 'id' : 'dataseries'\n",
"\n",
" # the event marker flags\n",
" }, {\n",
" type : 'flags',\n",
" 'data' : [{\n",
" 'x' : 1,\n",
" 'title' : 'H',\n",
" 'text' : 'Euro Contained by Channel Resistance'\n",
" }, {\n",
" 'x' : 2,\n",
" 'title' : 'G',\n",
" 'text' : 'EURUSD: Bulls Clear Path to 1.50 Figure'\n",
" }, {\n",
" 'x' : 3,\n",
" 'title' : 'F',\n",
" 'text' : 'EURUSD: Rate Decision to End Standstill'\n",
" }, {\n",
" 'x' : 4,\n",
" 'title' : 'E',\n",
" 'text' : 'EURUSD: Enter Short on Channel Break'\n",
" }, {\n",
" 'x' : 5,\n",
" 'title' : 'D',\n",
" 'text' : 'Forex: U.S. Non-Farm Payrolls Expand 244K, U.S. Dollar Rally Cut Short By Risk Appetite'\n",
" }, {\n",
" 'x' : 6,\n",
" 'title' : 'C',\n",
" 'text' : 'US Dollar: Is This the Long-Awaited Recovery or a Temporary Bounce?'\n",
" }],\n",
" 'onSeries' : 'dataseries',\n",
" 'shape' : 'circlepin',\n",
" 'width' : 16\n",
" }]\n",
" }\n",
"\n",
"\n",
"hc(config)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
" <script id=\"289279\" type=\"text/python\">\n",
"from javascript import JSConstructor\n",
"from browser import window\n",
"from datetime import datetime\n",
"\n",
"Highcharts = window.Highcharts\n",
"\n",
"#This doesn't work.\n",
"hc = JSConstructor(Highcharts.StockChart)\n",
"#hc = JSConstructor(Highcharts.Chart)\n",
"\n",
"config = {\n",
" 'chart': {\n",
" 'renderTo': 'hc_test'\n",
" },\n",
" 'rangeSelector': {\n",
" 'selected' : 1\n",
" },\n",
" 'title' : {\n",
" 'text': 'USD to EUR exchange rate'\n",
" },\n",
" 'tooltip': {\n",
" 'style': {\n",
" 'width': '200px'\n",
" },\n",
" 'valueDecimals': 4\n",
" },\n",
"\n",
" 'yAxis': {\n",
" 'title': {\n",
" 'text': 'Exchange rate'\n",
" }\n",
" },\n",
" 'series': [{\n",
" 'name' : 'USD to EUR',\n",
" 'data' : [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],\n",
" 'id' : 'dataseries'\n",
"\n",
" # the event marker flags\n",
" }, {\n",
" type : 'flags',\n",
" 'data' : [{\n",
" 'x' : 1,\n",
" 'title' : 'H',\n",
" 'text' : 'Euro Contained by Channel Resistance'\n",
" }, {\n",
" 'x' : 2,\n",
" 'title' : 'G',\n",
" 'text' : 'EURUSD: Bulls Clear Path to 1.50 Figure'\n",
" }, {\n",
" 'x' : 3,\n",
" 'title' : 'F',\n",
" 'text' : 'EURUSD: Rate Decision to End Standstill'\n",
" }, {\n",
" 'x' : 4,\n",
" 'title' : 'E',\n",
" 'text' : 'EURUSD: Enter Short on Channel Break'\n",
" }, {\n",
" 'x' : 5,\n",
" 'title' : 'D',\n",
" 'text' : 'Forex: U.S. Non-Farm Payrolls Expand 244K, U.S. Dollar Rally Cut Short By Risk Appetite'\n",
" }, {\n",
" 'x' : 6,\n",
" 'title' : 'C',\n",
" 'text' : 'US Dollar: Is This the Long-Awaited Recovery or a Temporary Bounce?'\n",
" }],\n",
" 'onSeries' : 'dataseries',\n",
" 'shape' : 'circlepin',\n",
" 'width' : 16\n",
" }]\n",
" }\n",
"\n",
"\n",
"hc(config)\n",
" </script>\n",
" <div id=\"brython_container_289279\"><div id=\"hc_test\" style=\"width: 700px; height: 300px;\"></div></div>\n",
" <script type=\"text/javascript\">brython({debug:1, static_stdlib_import: false, ipy_id: [\"289279\"]});</script>\n"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML object>"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment