<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Python-Programming on Vincent Frimpong</title>
    <link>https://blog.aduuna.dev/tags/python-programming/</link>
    <description>Recent content in Python-Programming on Vincent Frimpong</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 04 Oct 2018 17:18:20 +0000</lastBuildDate>
    <atom:link href="https://blog.aduuna.dev/tags/python-programming/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Let&#39;s take it online — Deploying your flask Application to PythonAnywhere</title>
      <link>https://blog.aduuna.dev/lets-take-it-online-deploying-your-flask-application-to-pythonanywhere/</link>
      <pubDate>Thu, 04 Oct 2018 17:18:20 +0000</pubDate>
      <guid>https://blog.aduuna.dev/lets-take-it-online-deploying-your-flask-application-to-pythonanywhere/</guid>
      <description>In this tutorial, we will take a simple flask app from our local machine to a server online. I assume it is your first time doing this and I try to explain...</description>
      <content:encoded><![CDATA[<p>In this tutorial, we will take <a href="https://github.com/puclubug/simple_flask_app">a simple flask</a> app from our local machine to a server online. I assume it is your first time doing this and I try to explain everything to the most basic level. Sorry to the Ninjas</p>
<h3 id="boring-talk">Boring Talk</h3>
<p>You can scroll to the tutorial Section if it gets boring here.</p>
<p><strong>So what the hell is a server?</strong></p>
<p>Let’s make it practical since we&rsquo;ve all used the internet before. When you visit a website to look for information, basically, you are a client on the internet and someone needs to serve you with that information. It is like this:</p>
<ul>
<li>Internet —Restaurants</li>
<li>You the client— Customer</li>
<li>Server — Waiter/Server</li>
<li>request for webPage — Order for food</li>
</ul>
<p>So when we build web apps with Flask, Django, or Pyramid, we are designing what and how to serve our customers so that a computer can serve our clients for us.</p>
<p><strong>Okay, so How Do I Serve?</strong></p>
<p>From what is said above we can tell that a server is just another computer. A computer that does one thing — Serve. Any computer could be a Server. I once used my raspberry pi as a staging server. When you are developing with Django or Flask, you have a development server running on your local machine.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ python -m SimpleHTTPServer 8000
</span></span></code></pre></div><p>This command starts a server at port 8000 in the directory it was run from.</p>
<p>But we need a constant power supply, A stable internet connection with a reasonable speed, and sometimes an ample storage space to keep our Restaurant open at all times. That is why we need the services of Server Companies to help us stay online.</p>
<h3 id="tutorial">Tutorial</h3>
<p>We have several options available to us because its 2018 and cloud computing is the hot cake for conglomerates. We will use PaaS/IaaS(Platform as a Service/Infrastructure as a Service) for our hosting because we get to skip operating system configurations, server setup, and focus on deploying fast. Plus it’s cheaper and mostly free. We have Google App Engine, Amazon Web Services, Heroku, and pythonanywhere as our favorite options today. <a href="https://blog.appdynamics.com/product/battle-of-the-paas-python-apps-in-the-cloud/">click here</a> if you want to read more about PaaS/IaaS.</p>
<h3 id="part-1-deploying-to-pythonanywhere">Part 1: Deploying to PythonAnywhere.</h3>
<p>For simplicity, let us all clone this simple app. and see it run locally. Use this only if you don’t have a working flask app but you want to follow the tutorial</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ git clone https://github.com/puclubug/simple_flask_app.git
</span></span><span class="line"><span class="cl">$ cd simple_flask_app
</span></span><span class="line"><span class="cl">$ git remote rm origin
</span></span><span class="line"><span class="cl">$ python main.py
</span></span></code></pre></div><p>You can visit <a href="http://localhost:8000/">http://localhost:8000/</a> in your browser to see it run and use CTRL+C to quit the server when done. If you don&rsquo;t have git installed, take a minute to get it <a href="http://git-scm.com">here</a>.</p>
<p>But If you have built your flask app locally on your machine, then in your app’s directory run this in your bash over there</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">echo &#34;# simple_flask_app&#34; &gt;&gt; README.md
</span></span><span class="line"><span class="cl">git init
</span></span><span class="line"><span class="cl">git add README.md
</span></span><span class="line"><span class="cl">git commit -m &#34;first commit&#34;
</span></span></code></pre></div><p><strong>Steps</strong></p>
<ol>
<li>Create accounts on <a href="http://github.com/join">GitHub</a> and <a href="https://www.pythonanywhere.com/ahl/">PythonAnywhere</a> if you haven&rsquo;t done that already and confirm your accounts in your email.</li>
<li>Go to your GitHub account and Add a new repository on GitHub — name it “simple_flask_app”. Leave everything as it is, only enter the name and click on create repository</li>
<li>In your flask app directory, open command prompt and run this but replace my github username — aduuna with yours</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">git remote add origin https://github.com/aduuna/simple_flask_app.git
</span></span><span class="line"><span class="cl">git push -u origin master
</span></span></code></pre></div><ol start="4">
<li>Let’s create a requirements.txt file, it contains a list of all the installed libraries needed for new python environments to run our code.</li>
</ol>
<p>New File: requirements.txt</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">Click==7.0
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">Flask==1.0.2
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">itsdangerous==0.24
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">Jinja2==2.10
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">MarkupSafe==1.0
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">Werkzeug==0.14.1
</span></span></code></pre></div><p>Those running this in a contained virtual environment can run</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ pip freeze &gt; requirements.txt
</span></span></code></pre></div><p>to get it automatically created.</p>
<ol start="5">
<li>Now let’s commit our changes</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ git add .
</span></span><span class="line"><span class="cl">$ git commit -m &#34;Added requirements.txt&#34;
</span></span><span class="line"><span class="cl">$ git push origin master
</span></span></code></pre></div><ol start="6">
<li>Head over to your dashboard on pythonanywhere and click on the blue “$ bash” button to start a bash console on the remote computer pythonanywhere just gave you when you created an account. Treat it just like you treat your bash on your local machine. let us clone our source code onto our new machine:</li>
</ol>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ git clone https://github.com/aduuna/simple_flask_app
</span></span></code></pre></div><p>Once again Don’t forget to change my username(aduuna) to yours.</p>
<ol start="7">
<li>click the web tab to add a new web app</li>
</ol>
<p><img alt="In the web Tab, add a new web\u00a0app" loading="lazy" src="https://blog.aduuna.dev/lets-take-it-online-deploying-your-flask-application-to-pythonanywhere/image-01.png"></p>
<p><em>In the web Tab, add a new web app</em></p>
<ul>
<li>Click Next</li>
<li>click Flask then Python3.6</li>
<li>change the path name to “/home/kingkunta/simple_flask_app/main.py”</li>
<li>click Next to complete</li>
</ul>
<ol start="7">
<li>Setup on pythonanywhere is as easy as:</li>
</ol>
<p>on your online console run</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">$ cd simple_flask_app
</span></span><span class="line"><span class="cl">$ mkvirtualenv --python=/usr/bin/python3.6 my-virtualenv
</span></span><span class="line"><span class="cl">$ workon my-virtualenv
</span></span><span class="line"><span class="cl">$ pip install --user -r requirements.txt
</span></span><span class="line"><span class="cl">$ git checkout main.py
</span></span></code></pre></div><ol start="8">
<li>We are done. Reload Website in the web Tab. And view your website online at <a href="https://kingkunta.pythonanywhere.com">https://kingkunta.pythonanywhere.com</a>.</li>
</ol>
<blockquote>
<p>Note: yours will be at <a href="https://your_username.pythonanywhere.com">https://your_username.pythonanywhere.com</a></p>
</blockquote>
<blockquote>
<p>All variables used here like “simple_flask_app”, “my-virtualenv” can of course be replaced by anything of your choice, just keep it simple.</p>
</blockquote>
<h2 id="summary">Summary</h2>
<p>All that we did was to upload our code from our desktop to github and then we download the same code to our computer at pythonanywhere and boom, we had our app online.</p>
<p>I will be back with <strong>Part 2</strong> where we use Heroku to host our webapp, I hope you enjoyed it. If you have any questions, corrections or additions you can send me a quick <a href="mailto:tossia13@gmail.com">email here</a> or leave a <a href="https://wa.me/233264236474">whatsapp message</a></p>
]]></content:encoded>
    </item>
  </channel>
</rss>
