snipt

Ctrl+h for KB shortcuts

Fancy

Python inside a Macro Scheduler Macro

/*
First ensure Python27.dll and imports are in your Macro Scheduler program folder.
Download and unzip this file:
https://www.mjtnet.com/software/python27.zip
*/

Let>url=http://ip.jsontest.com/

/*
python_code:

import urllib2
import json

# grab data from http://ip.jsontest.com/ - see www.jsontest.com
response = urllib2.urlopen('%url%')

# load the json
dict = json.loads(response.read())

# get the ip member
myip = dict["ip"]

# make a nice string representation of the dict
sdict = json.dumps(dict)

# Anything we print to IO is returned in the PYExec output var
print "All Done"
*/

//Load the Python code to a variable
LabelToVar>python_code,pcode

//Run the code and request the values of the sdict and myip variables ...
PYExec>pcode,output,sdict,myip

//Display the IP address
MessageModal>Your pubic IP is: %myip%
https://snipt.net/embed/9cef7a0f8e46de79a7a8c644058c81cf/
/raw/9cef7a0f8e46de79a7a8c644058c81cf/
9cef7a0f8e46de79a7a8c644058c81cf
fancy
Fancy
38
2019-07-16T10:06:09
True
False
False
/api/public/snipt/137000/
python-inside-a-macro-scheduler-macro-d8dd78f1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a> <a href="#L-21">21</a> <a href="#L-22">22</a> <a href="#L-23">23</a> <a href="#L-24">24</a> <a href="#L-25">25</a> <a href="#L-26">26</a> <a href="#L-27">27</a> <a href="#L-28">28</a> <a href="#L-29">29</a> <a href="#L-30">30</a> <a href="#L-31">31</a> <a href="#L-32">32</a> <a href="#L-33">33</a> <a href="#L-34">34</a> <a href="#L-35">35</a> <a href="#L-36">36</a> <a href="#L-37">37</a> <a href="#L-38">38</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="p">/</span><span class="o">*</span> </span><span id="L-2"><a name="L-2"></a><span class="no">First</span> <span class="n">ensure</span> <span class="no">Python27</span><span class="o">.</span><span class="n">dll</span> <span class="n">and</span> <span class="n">imports</span> <span class="n">are</span> <span class="n">in</span> <span class="n">your</span> <span class="no">Macro</span> <span class="no">Scheduler</span> <span class="n">program</span> <span class="n">folder</span><span class="o">.</span> </span><span id="L-3"><a name="L-3"></a><span class="no">Download</span> <span class="n">and</span> <span class="n">unzip</span> <span class="n">this</span> <span class="nf">file:</span> </span><span id="L-4"><a name="L-4"></a><span class="nf">https:</span><span class="sr">//</span><span class="n">www</span><span class="o">.</span><span class="n">mjtnet</span><span class="o">.</span><span class="n">com</span><span class="sr">/software/</span><span class="n">python27</span><span class="o">.</span><span class="n">zip</span> </span><span id="L-5"><a name="L-5"></a><span class="o">*/</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="nf">Let&gt;url=http:</span><span class="sr">//i</span><span class="n">p</span><span class="o">.</span><span class="n">jsontest</span><span class="o">.</span><span class="n">com</span><span class="p">/</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a><span class="p">/</span><span class="o">*</span> </span><span id="L-10"><a name="L-10"></a><span class="nf">python_code:</span> </span><span id="L-11"><a name="L-11"></a> </span><span id="L-12"><a name="L-12"></a><span class="n">import</span> <span class="n">urllib2</span> </span><span id="L-13"><a name="L-13"></a><span class="n">import</span> <span class="n">json</span> </span><span id="L-14"><a name="L-14"></a> </span><span id="L-15"><a name="L-15"></a><span class="c1"># grab data from http://ip.jsontest.com/ - see www.jsontest.com</span> </span><span id="L-16"><a name="L-16"></a><span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="ss">&#39;%url%</span><span class="err">&#39;</span><span class="p">)</span> </span><span id="L-17"><a name="L-17"></a> </span><span id="L-18"><a name="L-18"></a><span class="c1"># load the json</span> </span><span id="L-19"><a name="L-19"></a><span class="n">dict</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span> </span><span id="L-20"><a name="L-20"></a> </span><span id="L-21"><a name="L-21"></a><span class="c1"># get the ip member</span> </span><span id="L-22"><a name="L-22"></a><span class="n">myip</span> <span class="o">=</span> <span class="n">dict</span><span class="o">[</span><span class="s">&quot;ip&quot;</span><span class="o">]</span> </span><span id="L-23"><a name="L-23"></a> </span><span id="L-24"><a name="L-24"></a><span class="c1"># make a nice string representation of the dict</span> </span><span id="L-25"><a name="L-25"></a><span class="n">sdict</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">dict</span><span class="p">)</span> </span><span id="L-26"><a name="L-26"></a> </span><span id="L-27"><a name="L-27"></a><span class="c1"># Anything we print to IO is returned in the PYExec output var</span> </span><span id="L-28"><a name="L-28"></a><span class="n">print</span> <span class="s">&quot;All Done&quot;</span> </span><span id="L-29"><a name="L-29"></a><span class="o">*/</span> </span><span id="L-30"><a name="L-30"></a> </span><span id="L-31"><a name="L-31"></a><span class="sr">//</span><span class="no">Load</span> <span class="n">the</span> <span class="no">Python</span> <span class="n">code</span> <span class="n">to</span> <span class="n">a</span> <span class="n">variable</span> </span><span id="L-32"><a name="L-32"></a><span class="no">LabelToVar</span><span class="o">&gt;</span><span class="n">python_code</span><span class="p">,</span><span class="n">pcode</span> </span><span id="L-33"><a name="L-33"></a> </span><span id="L-34"><a name="L-34"></a><span class="sr">//</span><span class="no">Run</span> <span class="n">the</span> <span class="n">code</span> <span class="n">and</span> <span class="n">request</span> <span class="n">the</span> <span class="n">values</span> <span class="n">of</span> <span class="n">the</span> <span class="n">sdict</span> <span class="n">and</span> <span class="n">myip</span> <span class="n">variables</span> <span class="o">...</span> </span><span id="L-35"><a name="L-35"></a><span class="no">PYExec</span><span class="o">&gt;</span><span class="n">pcode</span><span class="p">,</span><span class="n">output</span><span class="p">,</span><span class="n">sdict</span><span class="p">,</span><span class="n">myip</span> </span><span id="L-36"><a name="L-36"></a> </span><span id="L-37"><a name="L-37"></a><span class="sr">//</span><span class="no">Display</span> <span class="n">the</span> <span class="no">IP</span> <span class="n">address</span> </span><span id="L-38"><a name="L-38"></a><span class="no">MessageModal</span><span class="o">&gt;</span><span class="no">Your</span> <span class="n">pubic</span> <span class="no">IP</span> <span class="nf">is:</span> <span class="o">%</span><span class="n">myip</span><span class="o">%</span> </span></pre></div> </td></tr></table>
"Macro Scheduler", Python