{"id":1911,"date":"2019-12-08T19:31:50","date_gmt":"2019-12-09T03:31:50","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=1911"},"modified":"2019-12-08T19:31:50","modified_gmt":"2019-12-09T03:31:50","slug":"python-flask-web-server","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/python-flask-web-server\/","title":{"rendered":"Python Flask Web Server"},"content":{"rendered":"\n<p>To start install MariaDB package for development<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install libmariadbclient-dev<\/pre>\n\n\n\n<p>Create Directory for site and templates directory<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir net\ncd net\nmkdir templates<\/pre>\n\n\n\n<p>Create a virtual environment called myenv<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m venv myenv<\/pre>\n\n\n\n<p>Activate the myenv python environment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">source myenv\/bin\/activate<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"139\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2019\/07\/pip_myenv_activate.png\" alt=\"\" class=\"wp-image-884\"\/><\/figure>\n\n\n\n<p>Check version of Python for virtual environment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano ~\/.profile<\/pre>\n\n\n\n<p>At the bottom of the file, type in the following line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">VIRTUALENVWRAPPER_PYTHON=\/usr\/bin\/python3.5<\/pre>\n\n\n\n<p>Exit the virtual environment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">source ~\/.profile<\/pre>\n\n\n\n<p>Install Flask in the virtual environment<\/p>\n\n\n\n<p>pip3 install flask<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"509\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2019\/07\/pip_install_Flask.png\" alt=\"\" class=\"wp-image-888\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Install the SQL python packages: <\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">pip3 install mysql\npip3 install mysql-connector  <\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"210\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2019\/07\/pip_install_mysql.png\" alt=\"\" class=\"wp-image-893\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create Database Schema<\/h3>\n\n\n\n<p>login to MariaDB as root and create database and tables<\/p>\n\n\n\n<p>mariadb -u root -p<br>CREATE DATABASE mydb;<br>USE mydb;<br>CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTO_INCREMENT, username CHAR(25), content CHAR(150));<br>INSERT INTO posts ( username, content) VALUES( &#8216;gilmedel&#8217;, &#8216;First Post&#8217;);<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Server application<\/h3>\n\n\n\n<p>Start by creating app.py file<br>Then create models.py file<br>python code:  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n<script src=\"https:\/\/gist.github.com\/gilmedel\/fa62b84c5881a4b3a2020313e3330669.js\"><\/script>\n\n\n\n<p>Create index.html in the template directory for Flask<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/gilmedel\/907f4e2db28c3e67987e4da62a26a5e3.js\"><\/script>\n\n\n\n<p>Save all files then run app.py to start the Python3,  Flask, MariaDB server <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 app.py<\/pre>\n\n\n\n<p>Navigate to the webpage http:\/\/localhost:5000<\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>To start install MariaDB package for development sudo apt-get install libmariadbclient-dev Create Directory for site and templates directory mkdir net cd net mkdir templates Create a virtual environment called myenv python3 -m venv myenv Activate the myenv python environment source myenv\/bin\/activate Check version of Python for virtual environment sudo nano ~\/.profile At the bottom of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/python-flask-web-server\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Python Flask Web Server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1911","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/1911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1911"}],"version-history":[{"count":0,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/1911\/revisions"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}