You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Python Quart app as shown below where the /update and /save routes return HTML for a paragraph div. The /update-editor route returns JSON that is used to update the text content of the Ace editor.
fromquartimportQuart, render_template, requestapp=Quart(__name__)
@app.get("/")asyncdefindex():
"""Main app page."""returnawaitrender_template("index.html")
@app.get("/update")asyncdefupdate():
"""Update the status message."""returnawaitrender_template("update.html")
@app.get("/update-editor")asyncdefupdate_editor():
"""Update text content in the Ace editor using JSON data."""return {"content": "New content is here."}
@app.post("/save")asyncdefsave():
"""Save the editor content."""form=awaitrequest.formcontent=form.get("content", "")
print("***")
print(content) # This could be saved to file or databaseprint("***")
returnawaitrender_template("save.html")
if__name__=="__main__":
app.run(debug=True)
The save.html template is here:
<strong>Status B:</strong> Saved text content from editor, check console
The update.html template is here:
<strong>Status A:</strong> Updated text content in the editor
The relevant content in index.html is shown below. The Ace editor lives in the #editor div. Notice the text content of the Ace editor is set with the editor.setValue function from JSON data.
<p>Update text content in the editor.</p><buttontype="button"
class="btn btn-primary mb-3"
hx-get="/update"
hx-target="#status"
>
Update Content
</button><p>Save text content from the editor.</p><buttontype="button"
class="btn btn-primary"
hx-post="/save"
hx-target="#status"
hx-vals="js:{content: editor.getValue()}"
>
Save Content
</button><pid="status"><strong>Status:</strong> Create a new note</p><divid="editor">Enter some text</div><script>vareditor=ace.edit("editor");editor.setTheme("ace/theme/monokai");editor.session.setMode("ace/mode/text");editor.renderer.setPadding(8);editor.renderer.setScrollMargin(8,8,0,0);editor.setOptions({fontSize: "13px",});asyncfunctionloadEditorValue(){constresponse=awaitfetch("/update-editor");constdata=awaitresponse.json();editor.setValue(data.content);editor.clearSelection();}document.body.addEventListener("htmx:afterSwap",asyncfunction(event){if(event.detail.target.id!=="status"){return;}constresponseUrl=newURL(event.detail.xhr.responseURL);if(responseUrl.pathname!=="/update"){return;}awaitloadEditorValue();});</script>
After the /update response and the htmx swap, the JSON data for the Ace editor is automatically fetched from the /update-editor route. Since I'm dealing with HTML and JSON responses, this seemed like the best approach. But I'm curious if there is a better way to handle this with htmx?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a Python Quart app as shown below where the
/updateand/saveroutes return HTML for a paragraph div. The/update-editorroute returns JSON that is used to update the text content of the Ace editor.The
save.htmltemplate is here:The
update.htmltemplate is here:The relevant content in
index.htmlis shown below. The Ace editor lives in the#editordiv. Notice the text content of the Ace editor is set with theeditor.setValuefunction from JSON data.After the
/updateresponse and the htmx swap, the JSON data for the Ace editor is automatically fetched from the/update-editorroute. Since I'm dealing with HTML and JSON responses, this seemed like the best approach. But I'm curious if there is a better way to handle this with htmx?Beta Was this translation helpful? Give feedback.
All reactions