Thursday, March 28, 2024

Python : Learning to Connect Between the OS Module and Flask

We will start to integrate Flask with other Python programming code, in this case between Flask and the OS module.


For the first one, we learn to use the OS module.


 import os  
 alamatFolder = os.getcwd()  
 print("Lokasi Folder Saat ini : ", alamatFolder)  


 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 import os  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman4.html")  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

This is the initial source code for displaying a message box from JavaScript, using a server made with Flask.


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .button {  
                     background-color: #04AA6D;  
                     border: none;  
                     color: white;  
                     padding: 15px 32px;  
                     text-align: center;  
                     text-decoration: none;  
                     display: block;  
                     font-size: 16px;  
                     margin: 10px 10px;  
                     cursor: pointer;  
                     -webkit-transition-duration: 0.4s;  
                     transition-duration: 0.4s;  
                }  
                .button2:hover {  
                     box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19)  
                }  
           </style>  
      </head>  
      <body>  
           <button class="button button2" type="button" onclick="tampilkanPesan()">Uji Coba OS dan Flask</button>  
           <script>  
                function tampilkanPesan() {  
                     alert("Menampilkan Kotak Pesan")  
                }  
           </script>  
      </body>  
 </html>  

We will try to build data delivery over the network using a RESTful API. In building the server and backend RESTFUL API, we use the Python programming language. We will use JavaScript to build the UI. JavaScript will also be used to consume JSON data sent through the RESTFUL API. JavaScript will be used to build AJAX technology.


 import json  
 from flask import Flask  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 # di bawah ini adalah source code untuk mengirimkan data dalam bentuk JSON ke jaringan dan ditampilkan di browser  
 def restApi1():  
   return json.dumps({'nama':'alesandro','e-mail':'alesandro@toto.com'})  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

The following is the source code for reading a text file. The results of the reading will then be displayed on the web page. Alternatively, the results of the reading can be sent using a RESTful API. Once the reading results are converted to JSON, they will be displayed on the web page using AJAX.


Python
 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   file = open('/home/steven/proyekFlask/latihan15/templates/file1.txt','r')  
   angka1 = file.read()  
   file.close()  
   return render_template("halaman5.html", angka1 = angka1)  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

HTML
 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <p>{{ angka1 }}<p>  
      </body>  
 </html>  

No comments:

Post a Comment