Skip to content

Latest commit

 

History

History
204 lines (157 loc) · 5.05 KB

babel-formatting.org

File metadata and controls

204 lines (157 loc) · 5.05 KB

Tests

Testing sessions:

x = 1
print(x*2)

Timer formatting

Rounds by default, and shows by default:

print(1)
import time
time.sleep(3)

Turn off timer-show to hide it

print(1)

Set :timer-rounded to no to get the full timer. (Also modifying the timer string here so that my expect tests will skip it.)

print(1)

Table formatting

Dataframes are printed as org tables

import pandas as pd
data = {
'Name': ['Joe', 'Eva', 'Charlie', 'David', 'Eva'],
'Age': [44, 32, 33,33, 22],
'City': ['New York', 'San Francisco', 'Boston', 'Paris', 'Tokyo'],
'Score': [92.5, 88.0, 95.2, 78.9, 90.11111]}
df = pd.DataFrame(data)
print(df)

To not print the index as idx, set something else as to be the index.

print(df.set_index("Name"))

This respects various pandas options:

Float formatting

pd.options.display.float_format = '{:.1f}'.format
print(df.set_index("Name"))

Max rows

pd.options.display.max_rows = 10
long_df = pd.DataFrame({'A': range(200)})
print(long_df)

Problem – hangs when printing large dataframes.

print_org_df sets max_rows to be 20 by default to avoid this issue.

import pandas as pd
long_df = pd.DataFrame({'A': range(400)})
print(long_df)

If we make the max_rows even modestly large, we run into it, depending on computing resources.

pd.options.display.max_rows = 200
long_df = pd.DataFrame({'A': range(200)})
print(long_df)

Printing multiple dataframes:

print(df)
print("Space between dataframes")
print(df)

In general space between dataframes requires ones below to be aligned. I have an advise function ( adjust-org-babel-results ) that does this, but it can be slow if there are many tables in the org file, so it can be disabled like this.

print(df)
print("Space between dataframes")
print(df)

Testing Tabulate

If Tabulate is available we can use it directly to formate the dataframe. This is built into pandas and the safer option.

#+name print_with_tabulate

import pandas as pd
data = {
'Name': ['Joe', 'Eva', 'Charlie', 'David', 'Eva'],
'Age': [44, 32, 33,33, 22],
'City': ['New York', 'San Francisco', 'Boston', 'Paris', 'Tokyo'],
'Score': [92.5, 88.0, 95.2, 78.9, 90.11111]}
df = pd.DataFrame(data)
print(df)

Images

mocks out python plotting to allow plots to be interspersed with printing, and allows multiple to be made. :)

import pandas as pd
import matplotlib.pyplot as plt
print("look!")
df = pd.DataFrame({"x": [0, 2, 3, 4, 5,6,7,], "y": [10, 11, 12, 13, 14,15,16]})
print(df)
df.plot(x="x", y="y", kind="line")
plt.show()
print("tada!")

Make : Alerts on finishing

Alerts on finishing

When this finishes, it alerts you in an emacs minibuffer, with a link back. You also get a system alert. (This requires libnotify to be installed.)

import time
print("waiting")
time.sleep(1)
print("finished")
import time
print("waiting")
time.sleep(1)
print("finished")

I also have it configured to send an alert for any cell that takes more than 10 seconds.

This parses the cell timer, so requires it to be enabled for that part to be on to work.

Change the cell timer display to leave it up for the parsing and delete the timer afterwards.