I want to print out the pandas DataFrame as html table and send it as email. For Gmail, the styling in header will be eliminated, so the only way I can take is inline styling. The method I am using now is just replacing the html code. For example,
styled_html = html_contents.replace('<table ', '<table style="border: 1px solid; text-align:center"')
styled_html = styled_html.replace('<th ', '<th style="background-color:navy; color:white; border:none; padding:5px"')
styled_html = styled_html.replace('<td', '<td style="border:none; padding:5px;"')
I wonder if there is a better way for styling the table?
By the way, I have tried df.style.set_table_styles(), but it requires further change on the system so I tried not to use.
Related
I am automatizing some reports with jupyter and exporting it to html. I have some large tables, and I want to add a scrollbar to rigthside of table. A similar question is here: How to get a vertical scrollbar in HTML output from Jupyter notebooks. Here are a reproducible example:
import pandas as pd
import numpy as np
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
np.random.seed(2021)
df = pd.DataFrame({'type': ['a'] * 50 + ['b'] * 50,
'var_1': np.random.normal(size=100),
'var_2': np.random.normal(size=100)})
I think it could be done using .css styling, but I'm having dificulties to apply it. Pandas have some useful methods for styling tables (https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html). Then, I tryed the following:
vscrollbar = {
'selector': 'div.output_html',
'props': ' height: 500px; overflow-y: scroll;'
}
df.style.set_table_styles([vscrollbar])
After that, I compile the notebook from CLI with
jupyter-nbconvert example.ipybn --to-html
The output don't show the right scrollbar. But if I inspect the html code, the style that I passed throw Pandas is there
...
<style type="text/css">
#T_e24f1_ table {
height: 500px;
overflow-y: scroll;
}
</style>
<table id="T_e24f1_">
<thead>
<tr>
<th class="blank level0"> </th>
<th class="col_heading level0 col0">type</th>
<th class="col_heading level0 col1">var_1</th>
<th class="col_heading level0 col2">var_2</th>
</tr>
</thead>
<tbody>
...
I don't know if I'm passing the right selector, or if jupyter is overwriting this style.
How can I get a right scrollbar for long tables in html output from jupyter nbconvert?
There is a list of things you should know.
selector is a method that could help you to select one or several elements in html. So, maybe, you need to learn about how to use it. For your example. It should be '', cause, if you ever checked what the code output in ipynb, you will see like this below.
#T_f2ffe_ div.output_html{
height: 500px;
overflow-y: scroll;
}
The first part #T_f2ffe_ is generated randomly by pandas and the second part div.output_html is what you write. And these selectors will work together, and that will cause you to not specify any elements
A table in html, its height is calculated in two aspects, the td height and the number of td. If you want the css height to work, you'd better add display:inline-block first.
So here is the final answer that will work by little change.
vscrollbar = {
'selector': '',
'props': ' height: 500px; overflow-y: scroll;display: inline-block;'
}
df.style.set_table_styles([vscrollbar])
And the html looks like.
An option to implement a vertical scrollbar is using the following code:
display(HTML("<div style='height: 200px'>" + df.style.render() + "</div>"))
You can change the layout and position of the scrollbar to whatever you want. The output looks like this:
I'm new to Materialize and Angular. I have the exact same question as the question in this thread Change the default color of materialize.css input fields. I have attached screenshot
However, the solutions do not answer the question. I implemented this code in styles.css:
input:focus {
border-bottom: 1px solid #005DAB !important;
box-shadow: 0 1px 0 0 #005DAB;
}
label:active {
color: #005DAB;
}
Here's what I'm seeing:
What I'm seeing is the bottom border changes to blue (which is what I wanted). However, the label changes to blue temporarily (I'm assuming while it's active) and then it goes back to teal.
How do I make the selected label remain blue (#005DAB).
Hey the problem here is that the default CSS rules of materialize outweigh the custom rule you have defined.
You can read more about this here :
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
In short the most specific rule overwrites the other so in order to make your change appear you need to make your rule more specific.
There's multiple ways of going about this like using the id in the selector or adding !important to your rule.
However these methods are not recommended, you can rewrite the original CSS rule or add a custom class to add weight to your selector
<div class="input-field col s12 label-color-alternate">
<input id="password" type="password" class="validate">
<label for="password" class="">Password</label>
</div>
For example I added a class "label-color-alternate" to the outer div, if we add this class to our selector it'll give us the necessary specificity.
div.row > div.input-field.label-color-alternate > input+label.active {
color: #005DAB;
}
You can of course experiment with the best way to write your selector and to which elements you want to add custom classes.
I hope this helps !
set this in your external css:
input[type=text]:not(.browser-default):focus:not([readonly]) {
border-bottom: 2px solid var(--yourcolor);
box-shadow: 0 0 0 0 var(--yourcolor);
}
Due to hidden="hidden" I cannot run automated test with Robot Framework.
Kindly suggest me some idea to resolve it.
HTML code:
<a _ngcontent-c8="" class="browse cursor-pointer" tabindex="0">Browse</a>
<input _ngcontent-c8="" id="file" style="border: 1px solid gray; cursor: pointer; margin: 5px; width: 300px;" accept=".png, .jpg, .jpeg, .gif, .tif, .tiff" type="file" hidden="hidden">
There's a workaround for that - make the element visible through javascript, just before interacting with it:
Execute Javascript document.getElementById('file').style.visibility='visible'
UPDATE:
If you want to set an attribute different from style, like in this case a custom one called hidden, you use a different js method:
Execute Javascript document.getElementById('file').setAttribute('hidden') = 'new_value'
, where "new_value" is the one you know will make it visible.
And if you want to remove it altogether, the call is
Execute Javascript document.getElementById('file').removeAttribute('hidden')
If someone is still struggling with the SyntaxErrors like me, here is the correct syntax for setAttribute that works for me:
Execute Javascript document.getElementById('file').setAttribute('attributeName', 'attributeValue');
And if you don't have the id attribute:
Execute Javascript document.getElementsByClassName('file')[0].setAttribute('attributeName', 'attributeValue');
FYI:The method getElementsByClassName return an array of elements.
I am using twitter bootstrap in my rails app. I am trying to get the table header to display in the same row as the table data. Something similar to this
<tr align="left"><th>Due Date:</th><td><%= #invoice.due_date %></td>
</tr>
How can I accomplish this in twitter bootstrap?
Bootstrap doesn't have anything to do with it.
For each <tr> in your table, just use <th> for the cells you want emphasised.
If you're saying you want the row dividers to become column dividers, that's a bit of custom CSS along the lines of...
.table-vertical tr
{
border: none !important;
}
.table-vertical th
{
border-right: 1px solid gray;
}
... and so on.
There is some drawbacks using textarea and input-text as input of text forms. textarea has a little annoying triangle in right-lower corner and input-text is a single-line input.
I try to have a input of text like the facebook update input form. The input auto resize after linebreaks. And the element or tag used was <div>. I said "used" because, after they redesigned Facebook, I can't figure-out which tag is used now. There is CSS property that enables the user to edit the text in a div element. I actually copied the CSS property, but now I lost it. Can someone tell me which CSS property it was? I have a weak memory that it began with the -webkit prefix though
If you use html5 you can use:
<div id="divThatYouCanWriteStuffIn" contenteditable>
<!-- you can write in here -->
</div>
If you couple this with the css:
#divThatYouCanWriteStuffIn {
min-height: 4em; /* it should resize as required from this minimum height */
}
To get rid of the 'annoying little triangle' in textareas:
textarea {
resize: none;
}
JS Fiddle demo of both ideas.
I know you can do this in javascript by doing getElementByID('mydiv').contentEditable='true';, but I do not know how this would be done in CSS
The Facebook update input field is a TEXTAREA element. The trick is to use the resize property:
textarea { resize:none; }
That will make the triangle disappear.
You should be able to add your style to a textarea like you do with tags like p, h1, h2 etc..
So you can target all textareas or ones with specific classes or ids on them
Example:
textarea {
font-size:11px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-weight:normal;
line-height:140%;
color:black;
margin:0 0 5px 5px;
padding:5px;
background-color:#999999;
border:1px solid black;
}
This example will target all textareas on the page.
Change textarea to .nameOfClass or #nameOfId if you want to target a class or an id.
Hope this helps.