connorferster / forallpeople

Python SI units library: your 'daily driver' for calculations.
Apache License 2.0
295 stars 39 forks source link

%%render (from handcalcs) and Physical.prefix() don't work in the same cell in Jupyter env #99

Open GJoe2 opened 5 months ago

GJoe2 commented 5 months ago

It seems Physical.prefix() and %%render doesn't work in the same cell, from the following code

import handcalcs.render
import forallpeople as si
from forallpeople import Physical as ph
si.environment('structural',top_level=True)

%%render 1
b=0.30*m
h=0.60*m
I_g = b*h**3/4
I_g.prefix('unity')

I got:

KeyError                                  Traceback (most recent call last)
Cell In[45], [line 1](vscode-notebook-cell:?execution_count=45&line=1)
----> [1](vscode-notebook-cell:?execution_count=45&line=1) get_ipython().run_cell_magic('render', '', "I_g.prefix('unity')\n")

File c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\interactiveshell.py:2541, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
   [2539](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2539) with self.builtin_trap:
   [2540](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2540)     args = (magic_arg_s, cell)
-> [2541](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2541)     result = fn(*args, **kwargs)
   [2543](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2543) # The code below prevents the output from being displayed
   [2544](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2544) # when using magics with decorator @output_can_be_silenced
   [2545](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2545) # when the last Python token in the expression is a ';'.
   [2546](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/IPython/core/interactiveshell.py:2546) if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\handcalcs\render.py:106, in render(line, cell)
    [104](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/render.py:104) # Do the handcalc conversion
    [105](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/render.py:105) renderer = hand.LatexRenderer(cell, user_ns_postrun, line_args)
--> [106](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/render.py:106) latex_code = renderer.render()
    [108](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/render.py:108) # Display, but not as an "output"
    [109](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/render.py:109) display(Latex(latex_code))

File c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\handcalcs\handcalcs.py:185, in LatexRenderer.render(self, config_options)
    [184](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:184) def render(self, config_options: dict = global_config._config):
--> [185](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:185)     return latex(
    [186](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:186)         raw_python_source=self.source,
...
   [1933](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:1933) param = line.replace(" ", "").split("=", 1)[0]
-> [1934](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:1934) param_line = deque([param, "=", calculated_results[param]])
   [1935](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/handcalcs/handcalcs.py:1935) return param_line

KeyError: "I_g.prefix('unity')"

Advice required