The tooltip example presented in the reference guide show the following examples of formatting:
hover.tooltips = [
("index", "$index"),
("(x,y)", "($x, $y)"),
("radius", "#radius"),
("fill color", "$color[hex, swatch]:fill_color"),
("foo", "#foo"),
("bar", "#bar"),
("baz", "#baz{safe}"),
("total", "#total{$0,0.00}"
The 3 examples {safe}, {$0,0.00} and "$color[hex, swatch]:fill_color" are not clear: where can i find some documentation on them?
Basically I would like to understand what is possible and what isn't.
At the moment (for instance) I have 1 input that us a very long string (its a newspaper article) that I would like to format so it only shows the x first characters.
Other example I have a field #datetime that is retrieving its value from a datetime index. At the moment the tooltip displays that value as a int64 character. How to use a format tool such as Timestamp.strftime("%d-%m-%Y") so that it shows the date time in human readable format?
But I would like to have a clearer overview of what is possible/how that aspect of bokeh works
Since this answer was originally posted, new work has gone into Bokeh to make things simpler. A datetime field can be formatted as a datetime directly by the hover tool, by specifying a formatter, e.g.:
HoverTool(tooltips=[('label', '#datetime{%F}')],
formatters={'datetime': 'datetime'})
It is no longer necessary to pre-format date fields in the data source as below (although it certainly still works). For more information see Formatting Tooltip Fields
OLD ANSWER:
This is still an open issue for the project:
https://github.com/bokeh/bokeh/issues/1239
However, given some other recent work, this should now be fairly easy to implement this feature in a natural way. I have scheduled the task for the next 0.12.6 milestone.
Also, although Bokeh has extensive and rich documentation, there are still occasional gaps. This happens to be one of them, unfortunately. I note that there is an open issue to improve this:
https://github.com/bokeh/bokeh/issues/2595
I've updated it to make sure it is also included in the 0.12.6 milestone.
In the mean time, your best option is to pre-format the data as you want it to appear in the tooltip in Python. Then add a column to your data source that has the formatted version, the configure the hover tool to display this column:
source.data['formatted_date'] = my_pretty_print(source.date['date'])
hover.tooltips = [ ("date", "#formatted_date") ]
Related
I really hate having spread out code, I am looking at a bunch of long code with parameters and arguments that are taking up way to much space.
def __init__(self,
network,
value_coef,
entropy_coef,
lr=None,
eps=None,
max_grad_norm=None,
conv=False):
Seems the guy who wrote it forced a 50 character line limit, I whole heartedly disagree. I would much rather it looked like this.
def __init__(self, network, value_coef, entropy_coef, lr=None, eps=None, max_grad_norm=None, conv=False):
There is also more nonsense like this which I would like to get rid of.
if self.conv:
grid_obs = rollouts.grid_obs[:-1]\
.view(-1, *rollouts.grid_obs.size()[2:])
dest_obs = rollouts.dest_obs[:-1]\
.view(-1, *rollouts.dest_obs.size()[2:])
obs = (grid_obs, dest_obs)
I am using VS code for the python and am an ex Intellij user and am missing all the built in code formatting code tools. Any one got any tips? I have been looking at autopep8 but it seems they are missing that functionality.
First, that's not 50 chars limit but 79 (as per pep8 conventions) and the way you would like to have it wouldn't be pep8 compliant as it's over 100 columns.
So, for the first snippet you can have it the way you don't like it (which is the correct way) or let your formatter know that you want the line-length to be over 79 columns.
For the second snippet you can remove the escape character \ and let the formatter do its job. I don't think it's 'nonsense' as you call it, but feel free to format it differently.
Autopep8 or Black both work very well and they are not missing any functionality.
Provided you installed one or the other, you have to add the proper key/value pair to your settings.json:
"python.formatting.provider": "autopep8" // (or "black")
If you use autopep8, for example, you can specify the line length you want (150 in your case) by adding this to your settings.json file:
"python.formatting.autopep8Args": [
"--line-length=150"
]
The same goes for black. In that case the value would be:
"python.formatting.blackArgs": [
"--line-length=150"
]
Formatting with that parameter will wrap your code to that amount.
You can format code with alt+shift+f (on a Mac) or right click on the editor and "Format Document".
The code snippet came from the official documentation of xlwings here and it is the setup for my question.
import xlwings as xw
sht = xw.Book().sheets[0]
sht.range('A1').value = [['Foo1', 'Foo2'], [1, 2]]
chart = sht.charts.add()
chart.set_source_data(sht.range('A1').expand())
chart.chart_type = 'line'
chart.name
Running print(chart.api) outputs the tuple below.
(<xlwings._xlwindows.COMRetryObjectWrapper at 0x1fcd60c9a90>, <xlwings._xlwindows.COMRetryObjectWrapper at 0x1fcd60c9f28>)
If I want to use the api attribute to do some basic chart manipulation like remove the legend and add a title, it only works if I do it to chart.api[1]. For instance the code below works fine. It removes the chart legend and adds a title.
chart.api[1].HasLegend = 0
chart.api[1].SetElement(2)
chart.api[1].ChartTitle.Text = 'A title'
However, anything I do to chart.api[0] yields an error, (for instance print(chart.api[0].HasLegend) yields an error). I can't understand what kind of object this is or how it is useful. I can't find anything regarding this in the official documentation.
Finally my question is: what is the object at the index 0 above? Please, help me grok what it is.
There is another post that addresses your question about the object at the index 0.
set chart name in Xlwings
The expression chart.api returns a tuple with two COM wrappers. I'm
not really sure why there are two COM wrappers, but it seems that you
need the second one to access the chart. Hence the use of chart.api[1]
here.
I am using Python 3.6 and Bokeh 0.13.
In hovering over a bar chart, I would like to display a float as a money string where there is no decimal amount and there is a money sign in front of the number. Below is what I have so far for the tooltip.
tooltip_net_rev = [("Net Revenue", "$#y{int}")]
Which renders a string like what's shown below. This gets me most of the way there except for the commas.
Net Revenue: $128451
Here is what I would like it to look like with commas.
Net Revenue: $128,451
Any help with the tooltip formatting would be appreciated. Thanks
AFAIK {int} is not a meaningful format specifier for Bokeh tooltips, and if it is displaying anything at all, that's only because the unknown specifier is being ignored. There is fairly detailed documentation on formatting hover tooltips in the Formatting Tooltip Fields section of the User's Guide. Offhand, it looks like you want something like:
tooltip_net_rev = [("Net Revenue", "%#y{0,0.00}")]
In Pandas, there is a new styler option for formatting CSS ( http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.core.style.Styler.html ).
Before, when I wanted to make my numbers into accounting/dollar terms, I would use something like below:
df = pd.DataFrame.from_dict({'10/01/2015': {'Issued': 200}}, orient='index')
html = df.to_html(formatters={'Issued': format_money})
format_money function:
def format_money(item):
return '${:,.0f}'.format(item)
Now I want to use the Style options, and keep my $ formatting. I'm not seeing any way to do this.
Style formatting for example would be something like this:
s = df.style.bar(color='#009900')
#df = df.applymap(config.format_money) -- Doesn't work
html = s.render()
This would add bars to my HTML table like so(Docs here: http://pandas.pydata.org/pandas-docs/stable/style.html):
So basically, how do I do something like add the bars, and keep or also add in the dollar formatting to the table? If I try to do it before, the Style bars don't work because now they can't tell that the data is numerical and it errors out. If I try to do it after, it cancels out the styling.
That hasn't been implemented yet (version 0.17.1) - but there is a pull request for that (https://github.com/pydata/pandas/pull/11667) and should come out in 0.18. For now you have to stick to using the formatters.
Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:
lbl.set_markup("<span font_desc='Tahoma 5.4'>%s</span>" % text)
This 1) requires me to set the font , 2) seems like a lot of overhead (having to parse the markup), and 3) would make it annoying to change the font size of buttons and such. Is there a better way?
If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums link which has the following snippet of the the gtkrc file):
style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"
(replace the font with the one you/user need)
Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.
YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.
In C, you can do:
gtk_widget_modify_font(lbl, pango_font_description_from_string("Tahoma 5.4"));
In PyGTK, I believe it's something like:
pangoFont = pango.FontDescription("Tahoma 5.4")
lbl.modify_font(pangoFont)