Hi. I have a Dash table with values like $3 (+29%) and I want to make it more readable by moving the parenthesis below. I’m currently using the function below:
Sure, you can use a function for format the data. But I may not be understanding your question. If you post a minimal example, I can help you with the formatting.
Yeah so in my example in the f-string: value1=3, value3=’+’ and value2=29%. As it is I get: 3 (+29%). I would like the parenthesis to go in the next line. I tried using return f'{value1}\n({value3}{value2})' but it’s not working even though if I hit print(f'{value1}\n({value3}{value2})' I get the intended result.
Sorry, that’s not a MWE. What I mean is to provide a minimal example of what you are trying to do that includes some sample data that can be copied and run. Then I can see where the problem is. For example, the problem may be where and how you return the formatted string. I can’t tell that from your example.
Here is a MWE. Where would you put your function to update the data?
import dash
import dash_table
import pandas as pd
df = pd.DataFrame(
{
"date": ["20210613", "20210614", "20210615"],
"user": ["3 \n (29%)", "3 \n (29%)", "3 \n (29%)"],
"machine": [1, 0, 3],
}
)
app = dash.Dash(__name__)
app.layout = dash_table.DataTable(
id="table",
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
style_cell={"whiteSpace": "pre-line"},
)
if __name__ == "__main__":
app.run_server(debug=True)
Hi. Unfortunately can’t help, but I have the same problem considering adding newlines in Dash. Tried both solution listed by @AnnMarieW, but no success. The only difference in comparison to code in solutions is that I bring the dataframe from sql query (pd.read_sql_query, pyodbc) in app_data file. In solution, used df is create manually.
Tried it with "\n, \n, \\n, \r\n and br-s but nothing works.
Just to be clear, if you copy and paste the MWE and run it without any changes, do you see the data displayed with the line breaks like in the image above?
If the MWE is working for you, but it’s not working with your data, you could try inspecting the element with the browser dev tools to see if the new line is in the table. Like in this example, you can see the data (highlighted in blue) is on two lines. This means that you have updated the data correctly to include the line break. If it shows on one line, then the data is not right.
In case you don’t want to use dash DataTable, just pure HTML table, you need to set Td() children as array with html.Br() in order to break lines. Adding just \n or to string did nothing in my case. So I did: