ติดตั้ง Django

เลือกเวอร์ชันของ Python ที่จะใช้

Django versionPython versions
2.23.5, 3.6, 3.7, 3.8 (added in 2.2.8), 3.9 (added in 2.2.17)
3.13.6, 3.7, 3.8, 3.9 (added in 3.1.3)
3.23.6, 3.7, 3.8, 3.9, 3.10 (added in 3.2.9)
4.03.8, 3.9, 3.10
4.13.8, 3.9, 3.10, 3.11 (added in 4.1.3)

ดาว์นโหลดและติดตั้ง Python จาก https://www.python.org/downloads/

Python versionMaintenance status End of support Release schedule
3.11bugfix2027-10 PEP 664
3.10bugfix2026-10 PEP 619
3.9security2025-10 PEP 596
3.8security2024-10 PEP 569

ติดตั้ง Django แบบ official release

$ python3 -m pip install Django

ตรวจสอบการติดตั้ง

>>> import django
>>> print(django.get_version())
4.1.7
$ python3 -m django --version
4.1.7

Python subprocess

subprocess.call() – เก่าแล้ว Older high-level API

ใช้ subprocess.check_call() , subprocess.run() แทน

ตัวอย่าง 1

echo คำว่า test1 test2 ลงไปที่ไฟล์ /tmp/out.txt

%python
import subprocess
e = 'echo "test1 test2" > /tmp/out.txt'
subprocess.call(e, shell=True)

ตัวอย่าง 2

ตั้งชื่อไฟล์ของ log เป็นวันเวลาปัจจุบัน ไว้ใต้ /tmp แล้วใส่ข้อความ Hello World! ลงไป ด้วยการ echo

%python
import datetime
import subprocess
 
run_time = datetime.datetime.now() + datetime.timedelta(hours=7)
tmp_file = '/tmp/' + run_time.strftime("%Y-%m-%dT%H:%M:%S") + '.log'
print(tmp_file) # /tmp/2023-02-21T13:23:52.log

e = f'echo "Hello World!" >> {tmp_file}'
subprocess.call(e, shell=True)

ตรวจสอบว่าได้สร้างไฟล์ขึ้นมามั๊ย

%sh ls -l '/tmp/'

ดูเนื้อหาในไฟล์

%sh cat '/tmp/2023-02-21T13:23:52.log'

ใช้ Pandas อ่าน Text file

%python
import pandas as pd

# read text file
df = pd.read_fwf('/tmp/2023-02-21T14:55:00.log')
print(type(df))
print(df)
print(df.count())

sqlite3

สร้างดาต้าเบส หรือเปิดดาต้าเบส

import sqlite3
con = sqlite3.connect("tutorial.db")

เตรียม cursor

cur = con.cursor()

สร้างตาราง

cur.execute("CREATE TABLE movie(title, year, score)")

ตรวจสอบตารางที่มีจาก sqlite_master

res = cur.execute("SELECT name FROM sqlite_master")
print(res.fetchone())
# ('movie',)

insert ข้อมูล

cur.execute("""
    INSERT INTO movie VALUES
        ('Monty Python and the Holy Grail', 1975, 8.2),
        ('And Now for Something Completely Different', 1971, 7.5)
""")
con.commit()

select ข้อมูล

res = cur.execute("SELECT score FROM movie")
print(res.fetchall())

insert ข้อมูลด้วย executemany()

data = [
    ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    ("Monty Python's The Meaning of Life", 1983, 7.5),
    ("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # Remember to commit the transaction after executing INSERT.
for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
    print(row)

Verify that the database has been written to disk by calling con.close() to close the existing connection

con.close()

Tool

Python – GPG

ทดสอบบน Ubuntu 20.04.5 LTS ใน WSL 2

  1. Generate a key
  2. Export keys
  3. Import keys
  4. List keys
  5. Encrypt a string
  6. Decrypt a string
  7. Encrypt a file
  8. Decrypt a file

Installation

ติดตั้ง gnupg (น่าจะติดตั้งอยู่แล้ว)

sudo apt-get install gnupg 
$ gpg --version
gpg (GnuPG) 2.2.19
libgcrypt 1.8.5
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/jack/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

ติดตั้ง python-gnupg

pip install python-gnupg

1. Generate a key

import os
import gnupg

os.system('rm -rf /home/jack/gpghome')
os.system('mkdir  /home/jack/gpghome')

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
input_data = gpg.gen_key_input(
    key_length=4096,
    name_email='mr.phaisarn@gmail.com',
    passphrase='my passphrase')
key = gpg.gen_key(input_data)
print(key)
84511C0301E2D51E59D79C91B5432E86F4B8925A

2. Export keys

export ค่า public key และ private key ไว้ในไฟล์ mykeyfile.asc

import gnupg

key='84511C0301E2D51E59D79C91B5432E86F4B8925A'

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
ascii_armored_public_keys = gpg.export_keys(key,passphrase='my passphrase')
ascii_armored_private_keys = gpg.export_keys(key, True, passphrase='my passphrase')
with open('mykeyfile.asc', 'w') as f:
    f.write(ascii_armored_public_keys)
    f.write(ascii_armored_private_keys)
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGNqDu4BEADY6n5U0Fs4YBhAejiPZu5hI5i3oYbscK5xQSSXvgkUwoQkmHKQ
JVv+Chq4+jCguBSlmVXXMNnk45F7OhjuxpfrXDSxQPivwchXTh0117UEtTi5jE2s
eZTP5fMSdHomdD4NgU9PfSUiSPNH/PGnSQ8AFstFdFff846ry+dPu3XJdbkkVtEP
Cbv6idGCa2u/ZMukj2o2ZbvOXNVj1M5RFOpqJ6m52UQXA1W8S/Qr3g7gJ41jTWE3
oOP1oSfw5IbuO6yTrhOjwELTM2BexXwUAEtW59A8e+1JVh1xtbiTRxmnC+WHZ1T1
RQ9a7rFbFyqmboZ5RE8wUX7SsklzPyXtZhCnsFfBt3OVJNyD5hPoZ4qq7ihGeIIa
OD2g2VDu+uHeJg4Vqd8YcafFruLs0WR0BAdliYHClhhGSTQ2mFK7jQDfAVZYuFhZ
kKqd7WV7++e/oyLViWxDvhBN1XQ7Wbw5o1hkgeH6cyFg9nX/psVecXCriOJJUyVh
AKeFypLgf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uXli918RecnM6pEcuZqR3sfI6BRswFXQW1h8T6Kl2N9E9
ewuHlxsX4ehWRJytxj6qZNJ03WbhzV98s7jX1IC+EqB0LnVUB3xR2hBliPHe8PTq
CEc7yw3K+5Qv3PRea4nKQ3ELV5VWfGBXyB/Xo5XKbAzFy7lE8wgO0ZLQfuQeB+Xc
CY8IutY=
=6b0l
-----END PGP PRIVATE KEY BLOCK-----

3. Import keys

import gnupg
from pprint import pprint

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
key_data = open('mykeyfile.asc').read()
import_result = gpg.import_keys(key_data)
pprint(import_result.results)
[{'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '0',
  'text': 'Not actually changed\n'},
 {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '0',
  'text': 'Not actually changed\n'},
 {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '16',
  'text': 'Not actually changed\nContains private key\n'}]

4. List keys

import gnupg
from pprint import pprint

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
public_keys = gpg.list_keys()
private_keys = gpg.list_keys(True)
print('public keys:')
pprint(public_keys)
print('private keys:')
pprint(private_keys)
public keys:
[{'algo': '1',
  'cap': 'escaESCA',
  'compliance': '23',
  'curve': '',
  'date': '1667895022',
  'dummy': '',
  'expires': '',
  'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'flag': '',
  'hash': '',
  'issuer': '',
  'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514',
  'keyid': 'B5432E86F4B8925A',
  'length': '4096',
  'origin': '0',
  'ownertrust': 'u',
  'sig': '',
  'sigs': [],
  'subkeys': [],
  'token': '',
  'trust': 'u',
  'type': 'pub',
  'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'],
  'updated': ''}]
private keys:
[{'algo': '1',
  'cap': 'escaESCA',
  'compliance': '23',
  'curve': '',
  'date': '1667895022',
  'dummy': '',
  'expires': '',
  'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'flag': '',
  'hash': '',
  'issuer': '',
  'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514',
  'keyid': 'B5432E86F4B8925A',
  'length': '4096',
  'origin': '0',
  'ownertrust': 'u',
  'sig': '',
  'sigs': [],
  'subkeys': [],
  'token': '+',
  'trust': 'u',
  'type': 'sec',
  'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'],
  'updated': ''}]

5. Encrypt a string

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com')
encrypted_string = str(encrypted_data)

print('ok: ', encrypted_data.ok)
print('status: ', encrypted_data.status)
print('stderr: ', encrypted_data.stderr)
print('unencrypted_string: ', unencrypted_string)
print('encrypted_string: ', encrypted_string)
ok:  True
status:  encryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2
[GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23
[GNUPG:] BEGIN_ENCRYPTION 2 9
[GNUPG:] END_ENCRYPTION

unencrypted_string:  Who are you? How did you get in my house?
encrypted_string:  -----BEGIN PGP MESSAGE-----

hQIMA7VDLob0uJJaAQ//czHG42eeN1rtFTo6qdvoHSa/B3uJNeFSobUi4gUCxceh
yVbZ0La8pnLMy2JmA2agYaOvC6OhAXfNOhPXV5jinMTvwGB+dKC1qaAYXMknQjyF
5whvmQeBbuQX8AoKxs7hAYmlQO8mrHDBoP2xSdY/AgLeqdlnbZNcDR+NIpoPJS8a
zr+KHYAhEyZT0Bm6FAw7DvhriB4nJRLeMLoIQKIJTN3ZeEdlcZAt5xYL24dZfz8/
4Qpn++ItqsCQfXJ1leJIIE/oB4E83/J4FZuS1xh7n9OomBT2fRG8hmGlSzFYepX/
jDzjApmA/R4HGd6BuJZdNy6SV2DERs2NlTRko98IDFdCIg2Jp7jsSGSZowsKD7z5
rIU110iwR6hRropsuX18jlRk6QM1Um+dSGbHXTUk1OzUDE5uC3hBZsB0s9M2q5DS
HN+EbyatT9hB9X5Obf7w9Vj+Jta9q5voL/riZ2d+GFr8oR0d594An7Dup12zBff/
kdV7DgibAqorCD1MvVePOpvYw2NdhukRluOsVtXySkcAGy8VssBLoW3kqilOvrhq
pCJUG157TnhKnYW0l4BJ7KVoNFwBGsJKzS0Drh5Iq6LmiqHsKv7neK9LkApDeehD
EhdNoGN79fPvEYmync2bbGbVq6F7czHADwVKqNv0aDxaCrFHASsSXkGNbqMQ+tnS
YgFfzEOmYoDR7UjEog/OrBJAnPSgDcwn3I0xuU3RtDaEvlHCt6aX+NHEyIUvsIe+
Don8uUXi4VkjO2uoCOyNgwndoigDuouh8NDv5DIWcKt+KMGPkgKEmfbOxJNA0Rl5
TK5E
=Jyon
-----END PGP MESSAGE-----

6. Decrypt a string

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com')
encrypted_string = str(encrypted_data)
decrypted_data = gpg.decrypt(encrypted_string, passphrase='my passphrase')

print('ok: ', decrypted_data.ok)
print('status: ', decrypted_data.status)
print('stderr: ', decrypted_data.stderr)
print('decrypted string: ', decrypted_data.data)
ok:  True
status:  decryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] ENC_TO B5432E86F4B8925A 1 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08
      "Autogenerated Key <mr.phaisarn@gmail.com>"
[GNUPG:] BEGIN_DECRYPTION
[GNUPG:] DECRYPTION_COMPLIANCE_MODE 23
[GNUPG:] DECRYPTION_INFO 2 9
[GNUPG:] PLAINTEXT 62 1667895570
[GNUPG:] PLAINTEXT_LENGTH 41
[GNUPG:] DECRYPTION_OKAY
[GNUPG:] GOODMDC
[GNUPG:] END_DECRYPTION

decrypted string:  b'Who are you? How did you get in my house?'

7. Encrypt a file

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.')
with open('my-unencrypted.txt', 'rb') as f:
    status = gpg.encrypt_file(
        f, recipients=['mr.phaisarn@gmail.com'],
        output='my-encrypted.txt.gpg')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)
ok:  True
status:  encryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2
[GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23
[GNUPG:] BEGIN_ENCRYPTION 2 9
[GNUPG:] END_ENCRYPTION
-----BEGIN PGP MESSAGE-----

hQIMA7VDLob0uJJaARAAhwNuaLm5/eSCV81Uw+H2E536AzzXm/yPxM0CIjLjYxnE
+GwgvBxJsYtuuwCElUfYkIKYNzAWOGGRje961Be3+g5kjFfphvl4I1Xzwb5RKdWa
i9raKNQ1/PLQ6Hr8Fc3yh08QcgLkihyRgVKZ3suTX+KRzWd3/SS8rjS27nigQk8C
IoIuwJIsBYpB8xbBDM+Sfo2XJm938Z7n9ZfdDiuO0hEWBo5qF85Vh0jY91BVyPqD
yGYcsivK/OqRG/VrPTrp01+2+jBn5HPZoPCnjEZdyJ3p+k/bnvAymKpKmPfEHrt3
4FwJ0EIfFROx6u2xBWhoRAhl5vLjIgRqccwEHD9+HrT3aFyeQhOKcQoV0TXy5Y0w
rYLRBny2oolCGW1NqZi43pjamQXXVpJLDjOsn8pzhdbFiSxUrFZj2WXET7ItQaVd
fQQnPxpOpj7LOOncLdZ0qfGMYjNNXGoKlaPfdtJy1yQlz8mUUdZZAjAdCpKpnfca
YxuFmzMy2gGBKsYYrLB2FH6HasRbjb5uQ1qO72vybuPWCBNkX5jgWHGLddNagxR7
9EwMnvu958Oy3TiFfrr7YKWPIOQXaLx8WsgXCCl2rFJKG4Hy81ZM5k7DDQzzQFGd
w+a5FWo/cthXm6qx2LSE2ByFyAerqwe/T/wkAYmrQLIJdxZIWxp5xsuPBS6W6oLS
WwEiSHXqJPjqKHKnZwS0W9kWq/68voNf03QlEkN0+Hm3PRmEFIJnpw19BtfQEoY3
Rv/XCQXE2G6NVMajvFj72fvFzTUUyVRa8m//nu2wg9vG5JMjJbThA8L8+5E=
=WZm8
-----END PGP MESSAGE-----

ถ้า import public key แล้วนำมาใช้ encrypt จะได้ error invalid recipient

ok:  False
status:  invalid recipient
stderr:  gpg: WARNING: unsafe permissions on homedir `/home/jack/gpghome'
gpg: EAECD251: There is no assurance this key belongs to the named user
[GNUPG:] INV_RECP 10 84511C0301E2D51E59D79C91B5432E86F4B8925A
gpg: [stdin]: encryption failed: Unusable public key

ให้เพิ่ม gpg.trust_keys()

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
gpg.trust_keys('84511C0301E2D51E59D79C91B5432E86F4B8925A', 'TRUST_ULTIMATE')
open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.')
with open('my-unencrypted.txt', 'rb') as f:
    status = gpg.encrypt_file(
        f, recipients='84511C0301E2D51E59D79C91B5432E86F4B8925A',
        output='encrypted.txt.gpg')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)

8. Decrypt a file

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
with open('my-encrypted.txt.gpg', 'rb') as f:
    status = gpg.decrypt_file(f, passphrase='my passphrase', output='my-decrypted.txt')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)
ok:  True
status:  decryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] ENC_TO B5432E86F4B8925A 1 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08
      "Autogenerated Key <mr.phaisarn@gmail.com>"
[GNUPG:] BEGIN_DECRYPTION
[GNUPG:] DECRYPTION_COMPLIANCE_MODE 23
[GNUPG:] DECRYPTION_INFO 2 9
[GNUPG:] PLAINTEXT 62 1667895671
[GNUPG:] PLAINTEXT_LENGTH 32
[GNUPG:] DECRYPTION_OKAY
[GNUPG:] GOODMDC
[GNUPG:] END_DECRYPTION

Joblib

Install

pip install joblib

การหา square root ด้วย Python

from math import sqrt
li = [sqrt(i ** 2) for i in range(10)]
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

ใช้ joblib

from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2)(delayed(sqrt)(i**2) for i in range(10))
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

verbose: int, optional

The verbosity level: if non zero, progress messages are printed. Above 50, the output is sent to stdout. The frequency of the messages increases with the verbosity level. If it more than 10, all iterations are reported.

%python
from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100)(delayed(sqrt)(i**2) for i in range(10))
print(li)

# [Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
# [Parallel(n_jobs=2)]: Done   1 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   2 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   3 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   4 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   5 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   6 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   7 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   8 out of  10 | elapsed:    0.5s remaining:    0.1s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s remaining:    0.0s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s finished
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

default backend เป็น loky เราสามารถเปลี่ยนเป็น threading , multiprocessing ได้

from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100, backend='threading')(delayed(sqrt)(i**2) for i in range(10))
print(li)

MkDocs

MkDocs is a fast, simple and downright gorgeous static site generator that’s geared towards building project documentation. Documentation source files are written in Markdown, and configured with a single YAML

Installation

To install MkDocs, run the following command from the command line:

pip install mkdocs
> mkdocs -h
Usage: mkdocs [OPTIONS] COMMAND [ARGS]...

  MkDocs - Project documentation with Markdown.

Options:
  -V, --version  Show the version and exit.
  -q, --quiet    Silence warnings
  -v, --verbose  Enable verbose output
  -h, --help     Show this message and exit.

Commands:
  build      Build the MkDocs documentation
  gh-deploy  Deploy your documentation to GitHub Pages
  new        Create a new MkDocs project
  serve      Run the builtin development server
> mkdocs --version
mkdocs, version 1.5.1 from C:\Project\tmp\mkdocs1\.venv\lib\site-packages\mkdocs (Python 3.10)

For more details, see the Installation Guide.

Creating a new project

Getting started is super easy. To create a new project, run the following command from the command line:

mkdocs new my-project
cd my-project
$ tree
.
├── docs
│   └── index.md
└── mkdocs.yml

There’s a single configuration file named mkdocs.yml, and a folder named docs that will contain your documentation source files (docs is the default value for the docs_dir configuration setting). Right now the docs folder just contains a single documentation page, named index.md.

ไฟล์ mkdocs.yml

site_name: My Docs

ไฟล์ docs/index.md

# Welcome to MkDocs

For full documentation visit [mkdocs.org](https://www.mkdocs.org).

## Commands

* `mkdocs new [dir-name]` - Create a new project.
* `mkdocs serve` - Start the live-reloading docs server.
* `mkdocs build` - Build the documentation site.
* `mkdocs -h` - Print help message and exit.

## Project layout

    mkdocs.yml    # The configuration file.
    docs/
        index.md  # The documentation homepage.
        ...       # Other markdown pages, images and other files.

MkDocs comes with a built-in dev-server that lets you preview your documentation as you work on it. Make sure you’re in the same directory as the mkdocs.yml configuration file, and then start the server by running the mkdocs serve command:

> mkdocs serve
INFO     -  Building documentation...
INFO     -  Cleaning site directory
INFO     -  Documentation built in 0.24 seconds
INFO     -  [21:14:45] Watching paths for changes: 'docs', 'mkdocs.yml'
INFO     -  [21:14:45] Serving on http://127.0.0.1:8000/

Open up http://127.0.0.1:8000/ in your browser, and you’ll see the default home page being displayed:

Fenced code blocks

The fenced code blocks extension adds an alternate method of defining code blocks without indentation.

The first line should contain 3 or more backtick (`) characters, and the last line should contain the same number of backtick characters (`):

```
Fenced code blocks are like Standard
Markdown’s regular code blocks, except that
they’re not indented and instead rely on
start and end fence lines to delimit the
code block.
```

With this approach, the language can optionally be specified on the first line after the backticks which informs any syntax highlighters of the language used:

```python
def fn():
    pass
```

```sql
SELECT * FROM FOO
```

```c
main() {
    print('Hello');
}
```

Note that fenced code blocks can not be indented. Therefore, they cannot be nested inside list items, blockquotes, etc.

Building the site

That’s looking good. You’re ready to deploy the first pass of your MkLorum documentation. First build the documentation:

mkdocs build

This will create a new directory, named site. Take a look inside the directory:

$ ls site
about  fonts  index.html  license  search.html
css    img    js          mkdocs   sitemap.xml

Notice that your source documentation has been output as two HTML files named index.html and about/index.html. You also have various other media that’s been copied into the site directory as part of the documentation theme. You even have a sitemap.xml file and mkdocs/search_index.json.

If you’re using source code control such as git you probably don’t want to check your documentation builds into the repository. Add a line containing site/ to your .gitignore file.

echo "site/" >> .gitignore

If you’re using another source code control tool you’ll want to check its documentation on how to ignore specific directories.

Python installed path

sys.executable

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

>>> import sys
>>> sys.executable
'C:\\Python310\\python.exe'

os.path.dirname(path)

Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().

>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python310'
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python38\\python.exe'

Set default Python

Go to This PC → Right-click → Click on Properties → Advanced System Settings.

You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.

You will see a top half for the user variables and the bottom half for System variables.

Check the System Variables and double-click on the Path (to edit the Path).

Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.

Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.

Python – List all files and directories in a directory

List all files and directories in a directory

from os import listdir
my_path = '/home/jack'
files_list = listdir(my_path)
print(files_list)

List all files in a directory

from os import listdir
from os.path import isfile, join
my_path = '/home/jack'
files_list = [f for f in listdir(my_path) if isfile(join(my_path, f))]
print(files_list)

List all directories in a directory

from genericpath import isdir
from os import listdir
from os.path import isfile, join
my_path = '/home/jack'
files_list = [f for f in listdir(my_path) if isdir(join(my_path, f))]
print(files_list)