Can't format datetime using Bokeh Hovertool with Custom HTML - bokeh

Using Bokeh to display charts within Flask my hovertools works except for dates, which are my x axis in all charts. I have tried a hundred permutations of the code and extensively reviewed the documentation and related questions but am missing some key element on how to use formatters within custom html. This question seems to address my exact question, but I do not see how the answer relates to the actual custom html:
bokeh hover tool - format date variable in custom html
The data is passed to bokeh as a pandas data frame then use ColumnDataSource within Bokeh. The index, and x values, are definitely datetime with Bokeh treating them as a datetime in other parts of the charting such as the x axis labels and ticks.
This is my current hovertools html within Python:
def create_hover_absolute():
"""Generates the HTML for the Bokeh's hover data tool on our graph."""
hover_html = """
<div class="plot-tooltip">
<div>
<span style="font-weight: bold;">$name</span>
</div>
<div>
$y{$,000}
</div>
<div>
Date: #date{%Y-%m}
</div>
</div>
"""
return HoverTool(tooltips=hover_html, formatters={'date': 'datetime'})
I have tried a variety of ways to enter the date including:
$x
$x with all sorts of formatting such as {%Y%b}
#date
# date with all sorts of formatting
I end up with the date as either a long integer (15 digits) or a mysterious three digit number with TIB. Like this:
screenshot showing hover with Date: 118TB%
I can make all of this work on the local Bokeh server when I do show(plot) but not in the custom html when passing to web page. How do I format the date within the html? How do I include the formatters?
thank you

All I needed to do, as pointed out in comment by Eugene Pakhomov was to change 'date' to '#date' in the formatters. Like this:
return HoverTool(tooltips=hover_html, formatters={'#date': 'datetime'})

Related

Create new emphasis command R Markdown

In R Markdown, to make a text bold, we just need to do:
**code**
The the word code shows in bold.
I was wondering if there is a way to create a new command, let's say:
***code***
That would make the text highlighted?
Thanks!
It is not easily possible to create new markup, but one can change the way existing markup commands are rendered. Text enclosed by three stars is interpreted as emphasized strong emphasis. So one has to change that interpretation and change it to something else. One way to do so is via pandoc Lua filters. We just have to match on pandoc's internal representation of emphasized strong text and convert it to whatever we want:
function Strong (strong)
-- if this contains only one element, and if that element
-- is emphasized text, convert it to highlighted text.
local element = #strong.content == 1 and strong.content[1]
if element and element.t == 'Emph' then
table.insert(element.content, 1, pandoc.RawInline('html', '<mark>'))
table.insert(element.content, pandoc.RawInline('html', '</mark>'))
return element.content
end
end
The above works for HTML output. One would have to define what "highlighted text" means for each targeted format.
See this and this question for other approaches to the problem, and for details of how to use the filter with R Markdown.

Escape text in Bokeh Tools

I have a Bokeh TapTool that opens a webpage
TapTool(callback=OpenURL(url='/mypage?key=#name'))
However, the text in the name column of my ColumnDataSource contains unsafe text. I would like to escape this text. Typically I use tornado.escape.url_escape for this.
Is there a way to apply a function like url_escape automatically or do I have to maintain a separate column of escaped text in my ColumnDataSource?
As of 0.12.10 there is no functionality to escape field values in the client, nor are CustomJSTransform models yet applicable to OpenURL. My suggestion would be a CustomJS callback for the tap tool that does the escaping and opens the URL (a couple of lines of JS total).

How can I use custom classes in chapter titles with Asciidoc epub3 converter?

In the adoc file I define a chapter header like:
== [big-number]#2064# Das Spiele-Labor
For HTML that translates to
<span class="big-number">2064</span>
For the epub-Version, converted with asciidoctor-epub, apparently the class is omitted. The code line in the converter.rb:
<h1 class="chapter-title">#{title_upper}#{subtitle ? %[ <small class="subtitle">#{subtitle_formatted_upper}</small>] : nil}</h1>
(/var/lib/gems/1.9.1/gems/asciidoctor-epub3-1.5.0.alpha.7.dev/lib/asciidoctor-epub3/converter.rb)
How can I get the class information over to the chapter-title to format the first number in a special way?
Or is there another way to solve this? (The first number of the chapter title should be large and CSS hasn't got a 'first-word' attribute)

Excel / CSV Merge Text and Cell Data for Wordpress Import

I have several Wordpress HTML pages for import through CSV/excel. One of the fields is content for the Wordpress page. Since these pages are all the same except for in 3 places (2 names, 1 IMG URL) I'm trying to be efficient and upload an excel with custom fields.
What I'd like to do is merge the IMG urls and Product Names into the appropriate spot in the Excel cell text so it's imported as a complete page. I'm trying to avoid all the cutting and pasting when adding 100's of similar pages with only a few different spots.
Any tips or advice on where I can accomplish this? I haven't been able to figure it out or find help online.
Cell Data Example:
<div id="productimage" style="float:left;width:380px;">
<img alt="alternate" src="imagesource" />
</div>
<div id="productspecs" style="float:left;padding-left:25px;">
<h2><strong>Product Name</strong></h2>
</div>
"Product Name", "alternate", and "imagesource" I have fields for in a spreadsheet .. I just don't know how to merge them into this Cell Data Example to auto-populate these new pages.
Thanks!
If I understand your question correctly, you have html in an Excel cell and you want to make parts of that html dynamic by referencing content in other cells of the workbook.
I assume that in your example you want to make the imagesource and the Product Name dynamic.
You can copy and paste the html into the Excel formula editor. You can increase its height, so you see more than one line at a time. The formula editor can handle line breaks.
If you want to build a string that contains double quotes, you will need to use two double quotes if the quote is inside the string and three double quotes in a row if it is at the beginning or end of a string. You can use the ampersand to concatenate strings and cell references.
With your specific example above, the formula in Excel would read somewhere along these lines (replace Sheet2!A2 etc. with the cell that holds your data. Arrange that data in a table with a row for each product, then you can copy this formula down to get the desired result.
="<div id=""productimage"" style=""float:left;width:380px;"">
<img alt=""alternate"" src="""&Sheet2!A2&""" />
</div>
<div id=""productspecs"" style=""float:left;padding-left:25px;"">
<h2><strong>"&Sheet2!B2&"</strong></h2>
</div>"
Turn on "Wrap Text" in the cell format, otherwise you will see it all in one line of code. The screenshot below uses two rows of data with different texts for image source and product name in sheet 2.
EDIT: I tried to post this in a comment, but the double and triple quotes don't make it and get replaced with just one quote.
Also, you managed to delete some of the & signs that concatenate the different strings. Please look again at the original formula I've posted. Replace the cell references with yours, but don't mangle the code. The principle is this:
="First String"&A1&"Next String"
If the string has quotes inside, double them
="He said "Please" but nobody heard him"&A1&"next string"
If the string has quotes at the beginning of the string, then you need the opening quote for the string and the double quote for the quote inside the string. Likewise for quotes at the end of the string: duplicate the quote in the string and then add the closing quote.
="""Please" - he said"&A1&"and she answered "OK."""

PHPExcel Accounting Formats

I'm working with PHPExcel and I'm trying to format a cell using Excel's built-in "Accounting" format. I'm aware of the Format Code:
PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE
But this simply formats to two decimal places and adds a $ in front of the number. The result I'm looking for is the right aligned cell with the $ on the left. $0 values should be listed as "-" and negative values should be $ (1.11)
As far as I can tell there are no other currency formats in the documentation (though I may have missed it, this documentation is horrendous). Am I looking in the wrong place? Can this be achieved with regular cell formatting or is Excel doing something unique for Accounting?
I reverse engineered the format code from an existing spreadsheet using PHPExcel and got this:
_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(#_)
This is the code Excel places on the cell when you select the "Accounting" format... or click that "$" toolbar button in Excel 2007.
If you didn't need currency symbol :
->setFormatCode("_(* #,##0.00_);_(* \(#,##0.00\);_(* \"-\"??_);_(#_)");
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#,##0.00");
or use
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#.##0,00");
Set in getStyle CELL.
There are no other pre-defined formats beyond those listed in PHPExcel_Style_NumberFormat, but you should be able to set the format code to any string that you could use when setting an MS Excel custom format...
e.g.
[green]$#,##0.00;[red]$(-#,##0.00)
As regards cell alignment, set this to right yourself, or don't set it at all.

Resources