Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Jinja support#199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
ShaheedHaque wants to merge4 commits intoreactive-python:main
base:main
Choose a base branch
Loading
fromShaheedHaque:srh_jinja_support

Conversation

ShaheedHaque
Copy link

@ShaheedHaqueShaheedHaque commentedOct 9, 2023
edited by Archmonger
Loading

Description

WORK IN PROGRESS: DO NOT MERGE.

This is incomplete support for Jinja2-based templates. I could use some pointers to finish the missing part (see comments at the end of src/reactpy_django/templatetags/jinja.py).

(also missing, docs, packaging and tests)

To reproduce the results to date requires the following example changes on the Django side...

  1. Configure template files ending in ".jinja" to be processed via Jinja:

    TEMPLATES= [    {"BACKEND":"django_jinja.backend.Jinja2",# Jinja backend'DIRS': [os.path.join(BASE_DIR,'templates')],"APP_DIRS":True,"OPTIONS": {'context_processors': ['django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',                ...            ],"match_extension": (".jinja"),# suffix for Jinja templates"app_dirname":"templates",# Can be set to "jinja2.Undefined" or any other subclass."undefined":None,"newstyle_gettext":True,"tests": {# "mytest": "path.to.my.test",            },"filters": {# "myfilter": "path.to.my.filter",            },"globals": {# "myglobal": "path.to.my.globalfunc",            },"constants": {# "foo": "bar",            },"extensions": ["jinja2.ext.do","jinja2.ext.loopcontrols","jinja2.ext.i18n","django_jinja.builtins.extensions.CsrfExtension","django_jinja.builtins.extensions.CacheExtension","django_jinja.builtins.extensions.TimezoneExtension","django_jinja.builtins.extensions.UrlsExtension","django_jinja.builtins.extensions.StaticFilesExtension","django_jinja.builtins.extensions.DjangoFiltersExtension","reactpy_django.templatetags.jinja.ReactPyExtension",# new extension            ],"bytecode_cache": {"name":"default","backend":"django_jinja.cache.BytecodeCache","enabled":False,            },"autoescape":True,"auto_reload":DEBUG,"translation_engine":"django.utils.translation",        },    },    {'BACKEND':'django.template.backends.django.DjangoTemplates',# Django templates.
  2. Add the new app:

    INSTALLED_APPS= [     ..."reactpy_django", ]
  3. File of components incomponents.py:

    fromreactpyimportcomponent,html@componentdefhello_world(recipient:str):returnhtml.h1(f"Hello{recipient}!")
  4. Test view inviews/react.py:

    fromdjango.shortcutsimportrenderdefjinja_view(request):returnrender(request,"my-template.html.jinja")
  5. Template for the viewtemplates/my-template.html.jinja:

    <!DOCTYPE html><html><body>{{ component("paiyroll.components.hello_world", recipient="World") }}</body></html>
  6. project/asgi.py

    fromreactpy_djangoimportREACTPY_WEBSOCKET_ROUTEapplication=ProtocolTypeRouter({'http':get_asgi_application(),'websocket':AuthMiddlewareStack(URLRouter(        ...+ [REACTPY_WEBSOCKET_ROUTE]     )
  7. And finallyproject/urls.py:

    from ...viewsimportreacturlpatterns= [     ...path("reactpy/",include("reactpy_django.http.urls")),path("react/",react.jinja_view), ]

Once Django is restarted, navigating to the view, you should see
Hello World!

Checklist:

Please update this checklist as you complete each item:

  • Tests have been developed for bug fixes or new functionality.
  • The changelog has been updated, if necessary.
  • Documentation has been updated, if necessary.
  • GitHub Issues closed by this PR have been linked.

By submitting this pull request you agree that all contributions comply with this project's open source license(s).

@ShaheedHaqueShaheedHaque requested a review froma team as acode ownerOctober 9, 2023 20:24
@ArchmongerArchmonger marked this pull request as draftOctober 9, 2023 21:40
@ArchmongerArchmonger linked an issueOct 9, 2023 that may beclosed by this pull request
@ShaheedHaque
Copy link
Author

OK, this now render the "Hello World!" correctly.

Feedback requested before thinking about tests/docs etc.

Copy link
Contributor

@ArchmongerArchmonger left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A few comments since I took a early peak at the draft.

@ShaheedHaque
Copy link
Author

Resolved last outstanding comment. And ran black.

If we are to proceed, I'd propose to squash the current commits before thinking about tests/packaging/docs etc. (FWIW, in that regard, I assume the appropriate packaging would be as something like "reactpy_django[jinja]"...but I'm not currently sure how to go about implementing that).

@Archmonger
Copy link
Contributor

Archmonger commentedNov 27, 2023
edited
Loading

Optional dependencies can be defined within the package insetup.py. For example:

{"extras_require": {"encryption": ["cryptography","pycryptodome"],  },  ...}

@ShaheedHaque
Copy link
Author

Optional dependencies can be defined within the package insetup.py. For example:

{"extras_require": {"encryption": ["cryptography","pycryptodome"],  },  ...}

Thanks, I'm more or less familiar with this bit. I was more thinking about the new file I have added (and any supporting test file), and whether that could/should be omitted unless "[jinja] was specified?

@Archmonger
Copy link
Contributor

Archmonger commentedDec 31, 2023
edited
Loading

I have some changes in one of my PRs that will simplify test configuration
#190

TLDR: Our test suite can now run multiple different Django settings.py files.

And yes, from the user's perspective we should have all the jinja dependencies be optional viareactpy_django[jinja]

@Archmonger
Copy link
Contributor

Archmonger commentedJan 8, 2024
edited
Loading

My PR has been merged. Allsettings_*.py files within the test app will now automatically run tests against them.

There shouldn't be anything blocking this PR anymore, so you'll need to do the following:

  1. Add the needed dependencies to thetest-env.txt
  2. Add the needed dependencies to theoptional extras using the[jinja] keyword.
  3. Copysettings_single_db.py to createsettings_jinja.py.
  4. Write someconditional tests within thetest folder.
    • We don't want to run jinja tests in other testing environments, so we'll need a flag withinsettings.py to conditionally run these tests.
    • Thismight extend to preventing all other tests to run within the Jinja environment. In a perfect world, we would have some way of magically re-using all our old tests within a Jinja test environment, not sure if that's feasible though. I'll let you figure that best solution to that.
  5. Write a new page of documentation based onthe current installation page.
    • Probably will require changing themkdocs.yml nav menu to look something like
    • Get Started >Add ReactPy to your ... >Django Project
    • Get Started >Add ReactPy to your ... >Django-Jinja Project

@ShaheedHaque
Copy link
Author

Noted. I've a full plate right now but have this on my radar.

@ArchmongerArchmonger changed the titleWIP Jinja support.Add Jinja supportJan 29, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ArchmongerArchmongerArchmonger left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Support Jinja2 templates
2 participants
@ShaheedHaque@Archmonger

[8]ページ先頭

©2009-2025 Movatter.jp