codelv / enaml-web

Build interactive websites with enaml
https://codelv.com/projects/enaml-web/
MIT License
99 stars 17 forks source link

Django integration 2: How to persist Atom object between user request #34

Closed youpsla closed 9 months ago

youpsla commented 2 years ago

Hello !!! I'm trying to integrate Enaml-web and Django using the CACHE system used ind dataframe example here:https://github.com/codelv/enaml-web/blob/37752a0221d1b9734d063daedbf39a3d9b9f51e3/examples/dataframe_viewer/app.py#L48

I don't achieve to persist this object between users interactions, in other works, after the answer has been sent back to the user browser.

Any idea to achieve that ?

Here is my simple view:


from django.http import HttpResponse

from web.core.app import WebApplication
import enaml

with enaml.imports():
    from .index import Index
enaml_web_app = WebApplication()

def testview(request):
    # I would like to cache index to be able to retrieve it later.
    index = Index()
    return HttpResponse(index.render())
youpsla commented 2 years ago

The solution I found as of now is to setup another server. Then, one Django server and one "Enaml-redering-and-cache-server". When Django receive a request, he send a request to the "Enamll" server and get the HTML (Partial or full page).

frmdstryr commented 2 years ago

Have you tried using django's cache framework to save the page?

Or more simply some global variable outside the request handler? Something like this:

CACHE = {}

def testview(request):
    cache_key = "index"
    data = CACHE.get(cache_key)
    if data is None:
        # page is not in cache, render it and save it in the cache
        print("Creating cache")
        index = Index()
        data = (index, index.render())
        CACHE[cache_key] = data 
    else:
        print("Using cache")
    index, page = data
    return HttpResponse(page)

A similar approach will work with the dataframe viewer example.

youpsla commented 9 months ago

I close this ticket because I solved the issue a long time ago and forgot it. Thanks again for help.