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}")]
Related
I am seeing that VS Code is getting intellisense hints from the comments I put in my class functions:
def GyroDriveOnHeading(self, desiredHeading, desiredDistance):
"""
Drives the robot very straight on a given heading for a \
given distance, using the acceleration and the gyro. \
Accelerates to prevent wheel slipping. \
Gyro keeps the robot pointing on the desired heading.
Minimum distance that this will work for is about 16cm.
If you need to go a very short distance, use move_tank.
Parameters
-------------
desiredHeading: On what heading should the robot drive (float)
type: float
values: any. Best if the desired heading is close to the current heading. Unpredictable robot movement may occur for large heading differences.
default: no default value
desiredDistance: How far the robot should go in cm (float)
type: float
values: any value above 16.0. You can enter smaller numbers, but the robot will still go 16cm
default: no default value
Example
-------------
import base_robot
br = base_robot.BaseRobot()
br.GyroDriveOnHeading(90, 40) #drive on heading 90 for 40 cm
"""
Which gives me a really nice popup when I use that function:
As you can see here, since I am about to enter the first parameter, desiredHeading, the intellisense was smart enough to know that the line in the comments under "Parameters" that starts with the variable name should be the first thing displayed in the hint. And indeed, once I type the first parameter and a comma, the first line of the intellisense popup changes to show the information about desiredDistance.
But I would like to know more about how the comments should be written. I read about the numpy style guide as being close to a standard most widely adopted, but when I change the parameter documentation format to match numpy (and somethihng called Sphinx has something to do with this too, I think), the popups were not the same. Really, I just want to see the documentation on how to document (yikes!) my python code so it renders correct intellisense. For example, how can I bold a word in the middle of a sentence? Are there other formatting options available?
This is just for a middle-school robotics club, nothing like production code for real programmers. Nothing is broken, I just want to learn more about how this works.
That's it for docstrings in python, about it's introduction:
https://docs.python.org/3.10/tutorial/controlflow.html#documentation-strings
https://peps.python.org/pep-0287/
In addition, you can use the type stub of the parameter in this way.
def open(url: str, new: int = ..., autoraise: bool = ...) -> bool: ...
I'm working on a project that involves exporting charts from SPSS. The value labels are supposed to be visible on the pie charts. Here's what I get, here's what I want.
The code is all working (see below how I do it). I just haven't found a way to make those "value labels" show up on the pie charts without going through the Chart Editor (double click on the chart in SPSS Viewer).
What I want is to have those labels on my pie charts like when I would click "Show Data Labels" as shown here. Is there any way to achieve this?
I'm accessing the charts in my code like this:
#in a SYNTAX file
* Encoding: UTF-8.
OMS
/DESTINATION
FORMAT=OXML
XMLWORKSPACE="my_ws"
VIEWER=YES
IMAGES=YES
IMAGEFORMAT=PNG
CHARTFORMAT=IMAGE.
BEGIN PROGRAM python.
import spss
spss.Submit("""FREQUENCIES VARIABLES=Sex
/PIECHART PERCENT
/ORDER=ANALYSIS.""")
imgs = spss.EvaluateXPath('my_ws', '/outputTree',
'//command[#command="Frequencies"]/chartTitle/chart/#imageFile' )
image=spss.GetImage( 'my_ws', imgs[-1] )
END PROGRAM.
OMSEND.
I don't think that's possible using SPSS syntax directly. The command syntax reference says about FREQUENCIES, subcommand PIECHART:
"PERCENT. [...] Percentage is displayed when you request values in the Chart Editor."
I've done that and it didn't add new code to the output document which makes me think that switching on percentages in the plot of this subcommand is not available through syntax.
You can however make a custom chart template as shown here.
Hence your code should be somthing like:
OMS
/DESTINATION
FORMAT=OXML
XMLWORKSPACE="my_ws"
VIEWER=YES
IMAGES=YES
IMAGEFORMAT=PNG
CHARTFORMAT=IMAGE.
BEGIN PROGRAM python.
import spss
spss.Submit("""set ctemplate
'<TEMPLATELOCAION>/<TEMPLATENAME>.sgt'.
FREQUENCIES VARIABLES=<VARIABLENAME>
/PIECHART PERCENT
/ORDER=ANALYSIS.
set ctemplate none.""")
imgs = spss.EvaluateXPath('my_ws', '/outputTree',
'//command[#command="Frequencies"]/chartTitle/chart/#imageFile' )
image=spss.GetImage( 'my_ws', imgs[-1] )
END PROGRAM.
OMSEND.
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") ]
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.
I use django-autocomoplete-light to make an autocopmlete-field for tags.
I decided to use TextWidget for my form because with ChoiceWidget I can't get the value from web-page. The problem is that when input field is activating, the values displays immediately in the drop-down. The ChoiceWidget works a little bit different. It starts display the values only when some characters are inputed.
Since you didn't provide some code I don't know exactly how your autocomplete registry code looks like, but specifying the minimum amount of characters to 0 does the trick.
Slightly modified example from the docs:
import autocomplete_light.shortcuts as al
al.register(your_model,
attrs={
'data-autocomplete-minimum-characters': 0,
},
)