ขั้นตอนการติดตั้ง Django ด้วย Python3 บน Windows

ขั้นตอนการติดตั้ง Django ด้วย Python3 บน Windows ติดตั้ง Python จาก https://www.python.org/downloads/ เปิด cmd โดย Run As Administrator ใช้คำสั่ง python -m pip install django ทดสอบโดยใช้คำสั่ง python -m django –version สร้าง Project ด้วยคำสั่ง django-admin startproject mysite เข้าไปใน project “mysite” directory ด้วยคำสั่ง cd mysite ทดสอบ runserver python manage.py runserver เปิด website: http://127.0.0.1:8000/ เดี๋ยวมาต่อเรื่อง การสร้าง App, การใช้ Database, การ Authentication และการสร้าง REST API เพื่อใช้งานกับ OAuth2

Read More »

วิธีใช้ Google Sheets เป็นฐานข้อมูล เพื่อการเฝ้าระวังระบบ โดยการใช้งานผ่าน Google API Client Library for Python

ต่อจาก วิธีการใช้ Google Sheets เป็นฐานข้อมูล การใช้งาน Google Drive API ด้วย Google Client Library for Python วิธีการ Upload ไฟล์ไปบน Google Drive File Stream ด้วย Google Client Library for Python คราวนี้ ใครมีข้อมูลที่เกิดขึ้นในเซิร์ฟเวอร์ภายในองค์กร แล้วต้องการส่งไปเขียนเก็บไว้ใน Google Sheets แบบต่อท้าย (Append) เช่น ในตัวอย่างนี้ วัดระยะเวลาการโอนย้ายข้อมูล เปรียบเทียบระหว่าง rsync เพื่อสำรองข้อมูลไปไว้ในเครื่องเซิร์ฟเวอร์สำรองที่ต่างวิทยาเขต กับ การนำไปเก็บไว้ใน Google Drive ตามวิธีการที่กล่าวไว้ใน วิธีการ Upload ไฟล์ไปบน Google Drive File Stream ด้วย Google Client Library for Python ผมได้เขียนโค๊ดเอาไว้ที่ https://github.com/nagarindkx/google.git สามารถโคลนไปใช้งานได้ (ช่วย Reference กันด้วยนะครับ) ขั้นตอนการใช้งานมีดังนี้ ใช้คำสั่ง git clone https://github.com/nagarindkx/google.git cd google ติดตั้ง python, pip, google-api-python-client ตามที่เขียนไว้ใน การใช้งาน Google Drive API ด้วย Google Client Library for Python และสร้างโปรเจคใน Google Developer Console เปิดใช้งาน Google Sheets API, สร้าง Credentials > OAuth Client ID แล้ว download JSON มาไว้ในชื่อว่า client_secret.json รูปแบบคำสั่งคือ $ python append2gsheet.py –help usage: append2gsheet.py [-h] [–auth_host_name AUTH_HOST_NAME] [–noauth_local_webserver] [–auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT …]]] [–logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] –data DATA –sheetid SHEETID [–range RANGE] [–value-input-option VALUEINPUTOPTION] optional arguments: -h, –help show this help message and exit –auth_host_name AUTH_HOST_NAME Hostname when running a local web server. –noauth_local_webserver Do not run a local web server. –auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT …]] Port web server should listen on. –logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL} Set the logging level of detail. –data DATA CSV format –sheetid SHEETID Google Sheets ID –range RANGE Simply Sheet Name like ‘Sheet1!A1’ –value-input-option VALUEINPUTOPTION Optional: [RAW,USER_ENTERED] สิ่งที่ต้องมี คือ Google Sheets ที่สร้างไว้แล้ว ให้สังเกตที่ URL ตัวข้อความที่อยู่หลัง https://docs.google.com/spreadsheets/d/ จะเป็น “Sheet

Read More »

วิธีการใช้ Google Drive เป็น Private Git Repository

git คือ distributed revision control system เรามักใช้ github.com สำหรับเก็บ source code แต่มัน public ซึ่ง บางทีเราก็ต้องการอะไรที่ private ต่อไปนี้ คือวิธีการใช้งาน Google Drive เพื่อสร้าง Private Git Repository ติดตั้ง Google Drive File Stream, git ให้เรียบร้อย ใน Google Drive สร้าง Directory ชื่อ git ขึ้นมาที่ My Drive กรณี Google Drive File Stream ก็จะเห็นที่ G:\My Drive\git จากนั้น ก็สร้าง Working Directory  เช่น ที่ Documents ในภาพ สร้างไว้ใน Documents\firebase\fmsworkshop จากนั้น คลิก Git Bash Here ใช้คำสั่ง git init เพื่อสร้าง .git ใน Directory นี้ก่อน จากนั้นใช้คำสั่งนี้ ที่มี –bare เพื่อสร้าง Remore Repository บน Google Drive git init –bare “G:\My Drive\git\fmsworkshop.git” บน Google Drive File Stream ก็จะมี fmsworkshop.git ปรากฏขึ้น ซึ่งต่อไปก็สามารถ git push ขึ้นไปเก็บไว้ได้แล้ว ต่อไป ก็พัฒนาโปรแกรมไป แล้ว ก็ git push ตามปรกติครับ git add . git commit -m “some text” git push “G:\My Drive\git\fmsworkshop.git” หวังว่าจะเป็นประโยชน์ครับ

Read More »

GCP #01 วิธีการสร้าง Virtual Machine บน Google Cloud Platform

ขั้นตอน มี Google Account ไปที่ https://console.cloud.google.com/start สำหรับคนที่ใช้ครั้งแรก ควรจะใช้สิทธิ์ Free Trial 300 USD / 12 Month ในการใช้งาน จะต้องมี Billing Account โดยต้องกรอกข้อมูล บัตร Credit/Debit ซึ่งต้องเป็น VISA/MasterCard เท่านั้น และต้องไม่ใช่ Prepaid ด้วย https://cloud.google.com/billing/docs/how-to/payment-methods#add_a_payment_method เมื่อเสร็จแล้วจะได้ Credit อย่างนี้ ต่อไป สร้าง Virtual Machine กัน ไปที่ เมนู > Cloud Launcher จะเห็นได้ว่า ตอนนี้ระบบจะสร้าง “My First Project” ไว้ให้ ซึ่งเราสามารถ สร้าง Project ใหม่ก็ได้ แต่ตอนนี้ใช้อย่างนี้ไปก่อน ต่อไป จะลองสร้าง Ubuntu Server ขึ้นมาใช้งาน ในที่นี้จะสร้าง Ubuntu 14.04 พิมพ์ Ubuntu ในช่องค้นหา แล้ว เลือก Ubuntu 14.04 จากนั้น คลิก Launch on Compute Engine ตั้งชื่อตามต้องการ (ถ้าต้องการ) สามารถเลือก Zone และ Machine Type ได้ตามความเหมาะสม ซึ่งจะมีราคาแตกต่างกัน ค่าเริ่มต้น Machine Type: n1-standard-1 จะให้ 1 vCPU, 3.75 GB RAM และ 10 GB standard persistent disk หากต้องการ Disk เพิ่ม สามารถคลิก Change เพื่อเพิ่มได้​ (standard persistent disk จะราคาถูกกว่า ssd มาก) ต่อไป กำหนดเรื่องเของ Firewall ถ้าให้บริการ HTTP/HTTPS ก็คลิกเลือกได้เลย ในกรณีที่ต้องการกำหนดค่าอื่นๆ เช่น Disk, Network, SSH Key ให้คลิก “Management, disks, networking, SSH keys” เสร็จแล้วกดปุ่ม Create รอสักครู่ ก็จะได้ VM มาใช้งานแล้ว ในที่นี้ จะได้ External IP ซึ่งใช้ในการติดต่อจาก Internet มา แต่หากมีการ Restart/Stop IP address นี้ก็จะเปลี่ยนไป (การ Fix มีค่าใช้จ่ายนิดหน่อย) และ การติดต่อไปยัง VM ก็สามารถทำได้ โดยการคลิก SSH ซึ่งสามารถเข้าถึงได้จาก Console นี้ หากต้องการใช้งานจาก Client อื่นก็ทำได้ แต่ต้องกำหนด SSH Key กันนิดหน่อย ซึ่งจะกล่าวในภายหลัง เพิ่มเติม ในกรณีต้องการเปิด Port เพิ่มที่ไม่ใช่ HTTP/HTTPS ให้คลิกที่ Menu > Network Services > Firewall Rules แล้วกำหนดค่าตามต้องการ โดยการ Create Firewall Rule หวังว่าจะเป็นประโยชน์ครับ

Read More »

Ambari #08 ปรับแต่ง pyspark ให้สามารถใช้งาน spark.ml ได้ ด้วย conda package management

เราสามารถใช้งาน Spark ในด้าน Machine Learning ด้วย pyspark แต่ปัญหาอยู่ที่ว่า python ที่ติดตั้งบน Ubuntu 14.04 นั้น ไม่มี package ที่จำเป็นต้องใช้ ได้แก่ numpy, scipy, scikit-learn, matplotlib ซึ่งขั้นตอนการติดตั้ง ก็จะยุ่งยาก เพราะต้อง compile code เองด้วย แต่ปัจจุบัน มีเครื่องมือที่เรียกว่า “conda” ทำหน้าที่ติดตั้ง package ที่ต้องการได้สะดวก ในที่นี้ จะเลือกใช้ python 2.7 และ จะติดตั้งลงไปใน /opt/conda ขั้นตอนการติดตั้ง conda ไปเลือก setup script จาก https://conda.io/miniconda.html ในการนี้ ขอให้ทำในสิทธิ์ของ root sudo su cd Download script wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh จากนั้น ใช้คำสั่งต่อไปนี้ เพื่อติดตั้ง conda ลงไปใน /opt/conda และ เลือกใช้ค่า default bash Miniconda2-latest-Linux-x86_64.sh -p /opt/conda -b ติดตั้ง scikit-learn package ซึ่งจะติดตั้ง package อื่นๆที่จำเป็นสำหรับ spark.ml เข้ามาด้วย /opt/conda/bin/conda install scikit-learn -y ทำขั้นตอน 3-6 กับ “ทุกๆ node” ใน Hadoop Cluster ต่อไปตั้งค่า Zeppelin ให้สามารถใช้งาน conda แทน python เดิม เปิด Zeppelin ขึ้นมา คลิก Interpreter > ค้นหา spark แล้วคลิก edit จากนั้น หาเลื่อนหาค่า pyspark.python แล้วแก้ไขเป็น /opt/conda/bin/python แล้วคลิก save จากนั้นก็จะสามารถใช้งาน spark.ml ได้แล้ว

Read More »