Django Templates In Jupyter - Avoid Kernel Restarts

Hi all,
I’m using django’s template engine for some small replacements in a LaTeX file.

Thanks to this great post and Ken’s answer it was really easy to implement. Everything works fine and my question is just coming from my curiosity to understand how things work django.

I’m working on Windows with Anaconda, using jupyter notebook.

My small project looks like that:

First cell:

import django
from django.conf import settings
from django.template import Engine, Context, Template
from django.template.loader import get_template

templates = [
  {'BACKEND':'django.template.backends.django.DjangoTemplates',
   'DIRS': ['my_template_folder'],
  }
]

settings.configure(TEMPLATES=templates)
django.setup()


— Some data is proceeded in the following columns —

Last cell:

context = {
# my context variables
}

t = get_template('my_source_LaTeX_file.tex')
with open('final_LaTeX_file.tex', 'w', encoding='utf-8') as output:
    output.write(t.render(context))

The last cell only works once.
When executing it initially, everything works as intended and the variables in the “my_source_LaTeX_file.tex” file are replaced and everything is written into the “final_LaTeX_file.tex” file.

But when I change things in my “my_source_LaTeX_file.tex” file and run the cell again, nothing changes anymore. I have to restart the kernel and run through every cell again.

Trying to solve the issue, I wanted to see what happends if I run the first cell once again. Then I receive the following exception:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
C:\Users\username\AppData\Local\Temp/ipykernel_5156/739752393.py in <module>
     19 ]
     20 
---> 21 settings.configure(TEMPLATES=templates)
     22 django.setup()
     23 

~\anaconda3\lib\site-packages\django\conf\__init__.py in configure(self, default_settings, **options)
    118         """
    119         if self._wrapped is not empty:
--> 120             raise RuntimeError('Settings already configured.')
    121         holder = UserSettingsHolder(default_settings)
    122         for name, value in options.items():

RuntimeError: Settings already configured.

…what makes kind of sense.

So I’m just curious: Does anyone have an idea why it needs a full python kernel restart to proceed changes in my .tex file when executing the last cell?

BR

Yes, the default behavior of the Django rendering engine is to read a template once, compile it, and keep the compiled template in memory. So when that template is accessed a second time, it’s read from cache and not re-read from the file.

(I know it’s documented somewhere, I know I’ve seen it - unfortunately, I’m somewhat embarassed to admit I can’t find the reference right now. The closest I can find is the counter-statement at The Django template language: for Python programmers | Django documentation | Django)

(Edit: Found the reference I was thinking of at The Django template language: for Python programmers | Django documentation | Django)

So taking a wag at it, I’d be inclined to try setting DEBUG=True in the settings file and see how it behaves.