Monday, December 24, 2018

How to use Custom CSS/Javascript in Android WebView


By default you cannot get custom CSS/Javascript loaded into Android Webview. We should override the onPageFinished method to be able to add custom CSS/javascript to Android Webview.





This example is using Kotlin, you may search something else if you're using native Java code or Flutter.





The best approach to use Custom CSS/Javascript is by creating the style node containing CSS and a script node containing the javascript for your sites.





var node = document.createElement('style');

node.type = 'text/css';
node.innerHTML = 'MY_CSS_STYLE';

document.head.appendChild(node);




and





var node = document.createElement('script');

node.type = 'text/javascript';
node.innerHTML = 'MY_JS_SCRIPT';

document.body.appendChild(node);




and override the onPageFinished method:





override fun onPageFinished(view: WebView, url: String) {
val code = """javascript:(function() {

var node = document.createElement('style');

node.type = 'text/css';
node.innerHTML = 'body {
color: white;
background-color: black;
}';

document.head.appendChild(node);

})()""".trimIndent()

loadUrl(code)
}





Sunday, December 23, 2018

How to Create Flask Server with Raspberry Pi


On this very basic tutorial, we gonna create a simple Flask Server with Raspberry Pi. Flask is python microframework which allows you to create a web based applications.





This time, we'll install Flask and using NGINX as the default webserver, altough the similar way can be reproduced using Apache.





Prepare your SD Card and burn the latest Raspbian Image onto the SD card using the tool like 'Etcher' or 'dd' command in Linux/MacOS.





Power on you raspberry pi, and take a moment, be sure your Raspberry Pi is connected to Internet, through Wi-Fi or LAN. Then update the repository and packages on Raspberry Pi with this command:





sudo apt-get update




Install the required software, like NGINX, pip3 (python installer) and uWSGI (micro-WSGI):





sudo apt-get install nginx
sudo apt-get install python3-pip
sudo pip3 install uwsgi




To install Flask Microframework, use the pip command:





sudo pip3 install flask




Create Flask App and Nginx Configuration





In your home directory, create new directory for flask app example, called 'flask'. Create new file, called 'testSite1.py' with nano containing this example script:





from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
return "<html><body><h1>Test site running under Flask</h1></body></html>"

if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True)




then, test your script with uwsgi:





uwsgi --socket 0.0.0.0:8000 --protocol=http -w testSite1:app




you should read this page: Test site running under Flask





Next step, configure uwsgi init file in uwsgi.ini in your flask directory:





[uwsgi]chdir = /home/pi/flasktest
module = testSite1:app

master = true
processes = 1
threads = 2

uid = www-data
gid = www-data

socket = /tmp/flasktest.sock
chmod-socket = 664
vacuum = true

die-on-term = true




Create Autostart uWSGI by create new in /etc/systemd/system, called uwsgi.service





[Unit]
Description=uWSGI Service
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/pi/flask/
ExecStart=/usr/local/bin/uwsgi --ini /home/pi/flask/uwsgi.ini

[Install]
WantedBy=multi-user.target




reload the daemon using sudo systemctl daemon-reload and then start the uwsgi service with systemctl:





sudo systemctl start uwsgi.service




configure NGINX to use uWSGI by editing the default site configuration in /etc/nginx/sites-available/default





Delete all content on those file, and replace with this script:





server {
listen 80;
server_name localhost;

location / { try_files $uri @app; }
location @app {
include uwsgi_params;
uwsgi_pass unix:/tmp/flasktest.sock;
}
}




Restart NGINX with sudo systemctl restart nginx command.





Test and look your browser.


Friday, December 21, 2018

How to Create FTP Server with Raspberry Pi


Raspberry Pi revolutionised electronics and computer hobby nearly 6 years. Too many creative project has been created with this tiny computer. On this simple tutorial, we'll create a simple FTP server with Raspberry Pi.





This tutorial as intended to Raspberry Pi 3 Model B user, but it also practicable to other version of Raspberry Pi with some modification.





You will need:





  • a Raspberry Pi 3 Model B or other version,
  • a SD Card with the latest Raspbian/ARMbian images or other Debian-based distribution for Raspberry Pi
  • Power supply
  • Keyboard or monitor or LCD




Install FTP Server





You must set your IP Address as static IP. To know your ip address, use command like 'ifconfig'. You also need to enable and start SSH Server for more easy to use in the future.





Install vsftpd using this command:





sudo apt-get update
sudo apt-get install vsftpd




Configure FTP Server





the default FTP server is located in /etc/vsftpd.conf.





Configure and tweak your ftp server by modifying some of those parameters. My suggesstion is enable this parameters, remove the # symbol on every lines that match following parameters:





anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
chroot_local_user=YES




then add some lines to the end of file:





user_sub_token=$USER
local_root=/home/$USER/ftp




Save and exit (in Nano you can use Ctrl + O and Ctrl + X





Last but not least, you can create the user FTP directory:





mkdir /home/pi/ftp
mkdir /home/pi/ftp/files
chmod a-w /home/pi/ftp




You can create new user account by using adduser command and create their FTP directory like the above commands.





Done.