Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Pretty printing.ipynb

23 views
Kernel: Python 2

This calculation is part of an assignment for a class I am taking. I need to calculate the incident power from a modified blackbody distribution. My professor is not familiar with Sage, and so I would like to be able to print clear and concise results so that he can read them without having to disect the code.

%load_ext sage import IPython.display as showme
# Constants #kB = 1.380648813e-23 # J/K kB = 8.617332478e-5 # eV/K #h = 6.6260695729e-34 # J s h = 4.13566751691e-15 # eV s Ts = 5760 # K Ta = 298 # K q = 1.60217656535e-19 # C c = 299792458 # m/s Fs = 6.8e-5

I need to make a correction factor so that the definite integral of the blackbody distribution from 280 nm to 4000 nm is 1000 W/m2W/m^2 instead of about ~1310 W/m2W/m^2. I first integrate the equation:

Incident Radiation=2qFskB4Tsh3c2∫x1x2x3ex−1dx\begin{equation} Incident~ Radiation = \frac{2qF_sk_B^4T_s}{h^3c^2} \int_{x_1}^{x_2}\frac{x^3}{e^x-1}dx \end{equation}

and then solve for the correction factor. I start out by setting up the upper and lower bounds of the integral.

# Set up the bounds for the integral. E2 = h*c/280e-9 E1 = h*c/4e-6 print 'The lower bound of the integral is ' + str(E1) + ' eV and the upper bound is ' + str(E2) + ' eV.' x2 = E2/(kB*Ts) x1 = E1/(kB*Ts)
The lower bound of the integral is 0.309960482591301 eV and the upper bound is 4.42800689416145 eV.

Using a print statement for the above equations is fine because the units don't have any special characters, superscripts, or subscripts. When I need to print something more complex, however, I can't get the formatting right.

# Answer to part b solarBB = 2*q*Fs*kB^4*Ts^4/(h^3*c^2)*numerical_integral(x^3/(e^x-1),x1,x2)[0] cf = 1000/solarBB print 'There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' print 'The correction factor to make this number be 1000 W/m^2 is ' + str(cf) + '.'
There is 1309.73965576525 W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at 5760 K. The correction factor to make this number be 1000 W/m^2 is 0.763510515695366.

As you can see, the units for W/m^2 are in plain text instead of being nicely formatted. I have tried several things to get both the value of the calculation and the properly displayed units, but have not found a satisfactory solution. Here are my attempts.

# Units are not formatted correctly print 'There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' print 'There is ' + str(solarBB) + ' \$W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.'
There is 1309.73965576525 $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at 5760 K. There is 1309.73965576525 \$W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.
#Nonsensical/errors print 'There is ' + str(solarBB) + latex('W/m^2') + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' print 'There is ' + str(solarBB) + show('W/m^2') + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' print 'There is ' + str(solarBB) + show(latex('W/m^2')) + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.'
There is 1309.73965576525 \text{\texttt{W/m{\char`\^}2}} of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-6a45769e534f> in <module>() 2 3 print 'There is ' + str(solarBB) + latex('W/m^2') + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' ----> 4 print 'There is ' + str(solarBB) + show('W/m^2') + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' 5 print 'There is ' + str(solarBB) + show(latex('W/m^2')) + ' of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' TypeError: cannot concatenate 'str' and 'NoneType' objects
#These all don't work and show unnecessary quotation marks. showme.display('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.display('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.display('There is ' + str(solarBB) + ' \$W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')
'There is 1309.73965576525 W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.'
'There is 1309.73965576525 $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.'
'There is 1309.73965576525 \\$W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.'
#These don't show anything? showme.display_markdown('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.display_html('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.display_latex('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')
#This works... showme.Markdown('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')

There is 1309.73965576525 W/m2W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.

#But only the last statement is output. This is a deal breaker when I would like to perform 2 calculations in one cell and print out both values. #I would need a separate code cell which for each value I needed to print. showme.Markdown('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.Markdown("Can't have more than one entry dummy!")

Can't have more than one line of text dummy!

#Also... showme.Markdown('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.' ' No new lines allowed.' '\n Nope.' '\newline Not for all the gold in Fort Knox.')

There is 1309.73965576525 W/m2W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at 5760 K. No new lines allowed. Nope. ewline Not for all the gold in Fort Knox.

#Desperation showme.Math('There is ' + str(solarBB) + ' W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')
Thereis1309.73965576525W/m2ofsolarradiationbetween280nmand4000nminaBBat5760K.There is 1309.73965576525 W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.
showme.Latex('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.') showme.Latex('Works, but I only get the last line again.')

Works, but I only get the last line again.

#What? Why is the out cell blank? This actually works in a Sage notebook. html('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')
<html><font color='black'>There is 1309.73965576525 <script type="math/tex">W/m^2</script> of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.</font></html>
#This doesn't work either. showme.display(html('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.'))
<html><font color='black'>There is 1309.73965576525 <script type="math/tex">W/m^2</script> of solar radiation between 280 nm and 4000 nm in a BB at 5760 K.</font></html>
#This also works in a Sage notebook. pretty_print('There is ' + str(solarBB) + ' $W/m^2$ of solar radiation between 280 nm and 4000 nm in a BB at ' + str(Ts) + ' K.')
<html><script type="math/tex">\newcommand{\Bold}[1]{\mathbf{#1}}\verb|There|\phantom{\verb!x!}\verb|is|\phantom{\verb!x!}\verb|1309.73965576525|\phantom{\verb!x!}\verb|$W/m^2$|\phantom{\verb!x!}\verb|of|\phantom{\verb!x!}\verb|solar|\phantom{\verb!x!}\verb|radiation|\phantom{\verb!x!}\verb|between|\phantom{\verb!x!}\verb|280|\phantom{\verb!x!}\verb|nm|\phantom{\verb!x!}\verb|and|\phantom{\verb!x!}\verb|4000|\phantom{\verb!x!}\verb|nm|\phantom{\verb!x!}\verb|in|\phantom{\verb!x!}\verb|a|\phantom{\verb!x!}\verb|BB|\phantom{\verb!x!}\verb|at|\phantom{\verb!x!}\verb|5760|\phantom{\verb!x!}\verb|K.|</script></html>

I wish I could just use a Markdown cell and report:

There is {{solarBB}} W/m2W/m^2 of solar radiation between 280 nm and 4000 nm in a BB at {{Ts}} K.

I think this would work if I had access to the IPython extension python-markdown.js. I did get it to work in a Sage notebook with HTML printint, but I am so invested in this now I can't let it go. It would still be handy to have access to variables and equations in from a Sage or Jupyter notebook in Markdown cells to avoid having to retype everything twice. I feel like this would work really well with Sage since everything is required to have a Latex representation anyway.

I might also be unaware of a simple solution, I'm pretty new to Sage and Python.