Note that there are some explanatory texts on larger screens.

plurals
  1. POCall Python function (Flask) from JavaScript file (melonJS) game on Facebook
    primarykey
    data
    text
    <p>I've looked through many topics on stackoverflow, github, Google and many more... and found many different answers although no simple answer.</p> <p>I'm writing a Facebook game in JavaScript (using melonJS engine). The framework is Flask (Python).</p> <p>What I am trying to do, is to have ability to send data between Python and JS code with no need to refresh the page (like database operations, email sending, etc.). I want the user just to see the game, choose options, play and the game does the rest.</p> <p>While I have managed to see that something like below will work:</p> <p>app.py</p> <pre><code>def add(f,l,a): g.db.execute('insert into persons (fname,lname,age) values (?, ?, ?)', [f,l,a]) g.db.commit() @app.route('/') def index(): cur = g.db.execute('select fname, lname, age from persons order by id desc') entries = [dict(fname=row[0], lname=row[1], age=row[2]) for row in cur.fetchall()] return render_template('app.html',entries=entries,addtodb=add) </code></pre> <p>app.html</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;THE GAME&lt;/title&gt; &lt;meta http-equiv="content-type" content="text/html;charset=utf-8" /&gt; &lt;meta name="generator" content="Geany 1.22" /&gt; &lt;script type="text/javascript" src="jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var adddb = '{{addtodb('Anne','Monk','12')}}'; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; {{addtodb('Allie','Monk','78')}} &lt;ul class=entries&gt; {% for entry in entries %} &lt;li&gt;&lt;h2&gt;{{ entry.fname }} {{ entry.lname|safe }} &lt;/h2&gt; Age: {{ entry.age }}&lt;/li&gt; {% else %} &lt;li&gt;&lt;em&gt;Unbelievable. No entries here so far&lt;/em&gt;&lt;/li&gt; {% endfor %} &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And both the {{addtodb}} will work. I just wonder how to send the function to file.js that will be only linked to the HTML (like jquery above), not put directly inside. The "{{...}}" will not work as I already checked. I suppose I have to find some module to Python or use AJAX maybe, except I have no idea where to start.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload