<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hassan Javaid on Medium]]></title>
        <description><![CDATA[Stories by Hassan Javaid on Medium]]></description>
        <link>https://medium.com/@ihassanjavaid?source=rss-c2d1827012fa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*u6DAS7N7EraT5W-_aaqmzQ.png</url>
            <title>Stories by Hassan Javaid on Medium</title>
            <link>https://medium.com/@ihassanjavaid?source=rss-c2d1827012fa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 16:20:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ihassanjavaid/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to run Python scripts on Flutter]]></title>
            <link>https://medium.com/@ihassanjavaid/how-to-run-python-scripts-on-flutter-d6a4aedb6227?source=rss-c2d1827012fa------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6a4aedb6227</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Hassan Javaid]]></dc:creator>
            <pubDate>Tue, 06 Oct 2020 20:03:05 GMT</pubDate>
            <atom:updated>2020-10-07T06:25:23.252Z</atom:updated>
            <content:encoded><![CDATA[<p>This article enables developers to link python scripts to flutter using Flask framework</p><figure><img alt="A picture containing flutter and python logos" src="https://cdn-images-1.medium.com/max/1024/1*RQQdxFYcaT3dFPGpsBVe9g.png" /><figcaption>Flutter and Python, using Flask</figcaption></figure><h3>Introduction</h3><p>There are many things being done in python that can’t be done in any other language, on top of which is Machine Learning, Data Mining, etc.</p><p>Python has some really helpful libraries like <a href="https://numpy.org/"><strong><em>numpy</em></strong></a><strong><em> </em></strong>and <a href="https://scikit-learn.org/"><strong><em>scikit</em></strong><em>-</em><strong><em>learn</em></strong></a><strong> </strong>which makes machine learning and data science work easy for the programmers.</p><blockquote><em>Code: </em><a href="https://github.com/ihassanjavaid/Iris-Predictor-Flutter-Flask"><em>https://github.com/ihassanjavaid/Iris-Predictor-Flutter-Flask</em></a></blockquote><h3><strong>How-to</strong></h3><p>Hence, any easy way to run python scripts on flutter application is by making an API, and calling it from within the flutter project through the <a href="https://pub.dev/packages/http">http </a>package.</p><h3><strong>Step-1:</strong></h3><p>Import http package from here:</p><p><a href="https://pub.dev/packages/http">http | Dart package</a></p><p>place that into your <em>pubspec.yaml</em> file and run `pub get`</p><pre><strong>Mac:</strong></pre><pre>$ flutter pub get</pre><pre><strong>Windows:</strong></pre><pre>&gt; flutter pub get</pre><h3><strong>Step-2:</strong></h3><p>In your flutter project create a new file, let’s say I name it <em>request.dart</em></p><p>Enter the following lines in that file</p><pre>import &#39;package:http/http.dart&#39;;</pre><pre>Future getData(url) async {</pre><pre>Response response = await get(url);</pre><pre>return response.body;</pre><pre>}</pre><h3><strong>Step-3:</strong></h3><p>Now, your flutter project is ready.</p><blockquote>Go to <a href="https://www.jetbrains.com/pycharm/">PyCharm IDE</a>, and create a new <strong>Flask</strong> Project.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*0o1vC8tsC8WjI6sRQdMJpA.png" /><figcaption>How to create a flask project</figcaption></figure><h3><strong>Step-4:</strong></h3><p>By default, you would have this code in your <em>app.py</em> file</p><pre>from flask import Flask<br><br>app = Flask(__name__)<br><br><br>@app.route(&#39;/&#39;)<br>def hello_world():<br>    return &#39;Hello World!&#39;<br><br><br>if __name__ == &#39;__main__&#39;:<br>    app.run()</pre><p>The above written code will be automatically generated when you will make a new Flask project. <strong>Do not attempt to delete any of this pre-written code.</strong></p><p>You can rename the <strong>hello_world() </strong>function and implement whatever logic you want and return a string, number or an array.</p><p>When you would run your project, it will run on localhost.</p><p>Normally, it runs on<strong> </strong><a href="http://127.0.0.1:5000/"><strong>http://127.0.0.1:5000/</strong></a> (local host, port 5000)</p><p>Before configuring flutter, you can also type this address on your browser to see if your project is up and running!</p><pre><a href="http://127.0.0.1:5000/"><strong>http://127.0.0.1:5000/</strong></a></pre><p>Now for getting the output in flutter, instead of simply returning string, we need to return a json.</p><p>For that,</p><pre>from flask import Flask, jsonify<br><br>app = Flask(__name__)<br><br><br>@app.route(&#39;/&#39;)<br>def hello_world():<br>    json_file = {}<br>    json_file[&#39;query&#39;] = &#39;hello_world&#39;<br>    return jsonify(json_file)<br><br><br>if __name__ == &#39;__main__&#39;:<br>    app.run()</pre><p>Now, instead of returning a string it is returning a json file like this</p><pre>{<br>&quot;query&quot;: &quot;hello_world&quot;<br>}</pre><h3>Step-5:</h3><p>Now in your main flutter file, where you want to access, add this code in the function you want to get your output by python script.</p><pre>var data = await getData(&#39;http://10.0.2.2:5000/);</pre><pre>var decodedData = jsonDecode(data);</pre><pre>print(decodedData[&#39;query&#39;]);</pre><p>The reason why you’re calling <a href="http://10.0.2.2:5000/">http://10.0.2.2:5000/</a> instead of <a href="http://127.0.0.1:5000/">http://127.0.0.1:5000/</a> is because for Android, the localhost’s address is this.</p><blockquote>There you go, you have your output stored in the variable <em>decodedData</em>.</blockquote><h3>Conclusion</h3><p>This is how python scripts and .ipynb files can be executed using flutter. Code is available on my <a href="http://github.com/ihassanjavaid">github</a> page.</p><p>Covering in the next article:</p><ol><li>How to give input to python script using http post method</li></ol><p><em>That’s all for today!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6a4aedb6227" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>