ptnega's technical blog

Giới thiệu về Kivy

1. Kivy là gì?

Kivy là một thư viện mã nguồn mở viết bằng Python để phát triển ứng dụng, đặc biệt là giao diện người dùng.

2. Kivy có thể chạy trên hệ điều hành nào?

Kivy có thể chạy trên hầu hết các hệ điều hành hiện nay, từ Windows cho tới Linux mà Mac OS. Kivy còn có thể chạy trên Raspberry Pi. Tất nhiên là trên Raspberry Pi thì sẽ cài Windows hay Linux.

3. Ứng dụng viết bằng Kivy có thể chạy trên nền tảng nào?

Kivy có tính đa nền tảng. Những ứng dụng viết bằng Kivy có thể chạy trên cả Windows, Linux, OS X tới Android hay iOS.

4. Kivy giúp chúng ta làm những gì?

Với Kivy, chúng ta có thể làm game và rất nhiều ứng dụng tương tác khác. Đặc biệt là trong bối cảnh mà cảm ứng đa điểm ngày càng đóng vai trò quan trọng thì Kivy cũng hướng tới điều này.

Mọi người có thể tìm thấy các project tạo bằng Kivy tại đây:

Kivy Gallery

5. Nói tóm lại thì Kivy có gì khác so với những thư viện/công cụ tương tự?

– Kivy được xây dựng 1 cách thuần túy. Có rất nhiều điều mới để khám phá ở Kivy…

– Kivy được viết bằng Python. Do đó nó rất nhanh và có độ chính xác cao. Ngoài Python thì Kivy cũng tự xây dựng ngôn ngữ riêng của mình là ngôn ngữ Kivy. Với ngôn ngữ Kivy thì chúng ta có thể tạo giao diện người dùng chỉ trong một số ít dòng code. Những luồng code rất rõ ràng và do đó cũng dễ để debug.

– Ngoài ra có những phần của Kivy được viết bằng C và Cython.

– Kivy có tính đa nền tảng, một điều rất cần thiết khi mà các thiết bị ngày nay ngày càng đa dạng trên nhiều nền tảng khác nhau.

– Hơn cả, Kivy là miễn phí. Tuy vậy, đối với một số nhà phát triển Kivy thì nó là cuộc sống của họ. Vì thế nên Kivy chắc chắn sẽ đi theo thời gian rất lâu nữa.

6. Một số liên kết hữu ích:

Cài đặt Kivy

Kivy API

Lập trình cơ bản với Kivy

Một số project với Kivy:


Chúc mọi người có những giây phút vui vẻ khi tìm hiểu Kivy 🙂

Advertisements
Leave a comment »

Loklak for the first time

I’ve just set up loklak server locally on my Ubuntu 15.10 and harvested more than 1M tweets. That’s interesting.

12

3

Query myself 😀

45

Here is the link to the data file of my collected tweet (just in case):

https://goo.gl/droNwk

Leave a comment »

Interpretation of binary searching algorithm

Problem: Given a random number in range [a, b] (inclusive). What is the maximum number of reasonable attempt the player has to make to get the number?

Let’s start with a more familiar problem: You’ve lost your cow on a rectangular grassland. Assume that the cow is at a fixed position on the land. How would you look for your cow?

This is my idea to find mine if I’m in that situation:

  1. Divide the land by 2, search for the 1st half.
    1. If the cow is there, I search no more.
    2. If it’s not, I will search in the 2nd half. But I will NOT search the whole 2nd half.
      1. I divide it by 2 (again) and search for the 1st new half. If the cow’s there, I search no more,
      2. and if it’s not, I will do the exactly same thing as I’ve done in the previous step.
      3. I will do this again and again until I find my cow. The searching ends when the line I used to divide the land is duplicated with one of those I’ve made in the previous steps. My cow will be on that line 😀

Do you see something that I’ve done the same in each step? Divide the land by 2. It’s exactly the concept of binary searching, that’s why they call it *binary* 😀

Each time you divide the range of searching by 2, you also reduce the time of searching by 2. If your answer is not in this half, then it must be in the other half! The searching ends when you cannot divide the searching range anymore. In other words, “Thisrange” / 2 = “Thisrange” so Thisrange must be zero (You search no more :D)

Each time you divide the range by 2, thus the number of attempts you’ve made is about log2(Range). More specific to say, the number of attempts equals to [log2(Range)] + 1 with [x] is the integral part of x. 2 ^ [log2(Range)] does not guarantee that you’ve searched all in the range. Since you cannot make “half of an attempt” or “quarter of an attempt”, you need to add 1 to your number of attempts.

Of course, that is the answer to the worst case that you need to run to every corner of the land to find your cow. If you’re a bit lucky, you are likely to find your cow in even 3, 2 or just 1 attempt.

To sum up, the maximum number of attempts you need to make to find a random number in range [a, b] is: [log2(b – a + 1)] + 1.

 

Leave a comment »

How to install Hugo on Linux and Windows

In this tutorial, I will show you how to properly install Hugo on your machine.

1. Installation on a Linux machine

  • Debian: In this section, I use my Ubuntu 15.10 64-bit as an example:

First, I need to download the appropirate package file from here:

https://github.com/spf13/hugo/releases

The file I need is hugo_0.15_amd64.deb since my system is 64-bit. If you use a 32-bit system, you will need hugo_0.15_i386.deb

Now I’ve got the package file in ~/Download/ on my machine. Navigate to this directory and use dpkg to simply install Hugo

cd ~/Downloads/

sudo dpkg -i hugo_0.15_amd64.deb

Screenshot from 2015-12-28 12-53-58

You might encounter problems about dependencies. The following command might help you:

sudo apt-get -f install

It will fix the issues of your installation and then install the package for you.

  • Arch Linux:

If you use Arch Linux, it’s quite easy to install Hugo since Hugo is available in yaourt

If you haven’t installed yaourt, add the following lines to /etc/pacman.conf:

[archlinuxfr]

SigLevel = Never

Server = http://repo.archlinux.fr/$arch

and run this command to install yaourt:

pacman -Sy yaourt

To install Hugo using yaourt, simply use this command:

yaourt hugo

You will be prompted to provide your password and everything will go in the right way.

In the end, you should be able to use hugo in your command line shell using the command hugo

 2. Installation on Windows

Depending on your system architecture – 32 bit or 64 bit – you need the correct setup file. I use my Windows 10 32-bit system as an example, so I’ll need hugo_0.15_windows_386_32-bit-only.zip

Those who use 64-bit Windows system should get hugo_0.15_windows_amd64.zip for installation.

When you’ve done downloading the setup file, extract it. Now you’re gonna add the directory of Hugo to PATH Environment variable so you can use Hugo everywhere on your system using CMD

Open Advanced system settings (This window usually appear when you right-click on My Computer and click on Properties):

2015-12-28 (1)

Click on Environment Variables:

2015-12-28 (2)

Now you should add a new variable for your user or modify the system variable. The variable to be created is PATH and the value of it is the path to the directory which contains the extracted files from the previous step. Note that values are separated by the semicolon (;) :

2015-12-28 (3)

2015-12-28 (4)

I’ve renamed the executeable file in the archive to hugo.exe for simple usage. Now I can use Hugo by typing hugo.exe <option> <params> wherever I want:

2015-12-28 (5)

If you have any problems during the installation, feel free to leave a comment in this post and I will answer as soon as possible.

1 Comment »

How to deploy a Hugo blog using Github Pages

In this post, I’m gonna show you how to use Github Pages to run your Hugo blog.

Assume that you have successfully built your blog locally and push the source to a repository on Github.

Check out the link to my post on How to install Hugo (Linux && Windows) if you haven’t installed Hugo on your machine:

https://ptnega.wordpress.com/2015/12/28/how-to-install-hugo-on-linux-and-windows/

If you haven’t got it on your machine, clone the repo to start working:

git clone https://github.com/ducphanduyagentp/ptnega-site.git

Now I will guide you step by step to build your site on Github Pages.

1. Build your blog in the working directory

In the config.toml file, there is an option called “baseurl”. In this post, we need to use it to run our site properly.

By default, your Github Pages are run at:

http://your-user-name.github.io/your-repo-name

This will absolutely your base URL for your Hugo blog. Add this line to the top of config.toml

baseurl = “http://your-user-name.github.io/your-repo-name&#8221;

For example:

baseurl = “http://ducphanduyagentp.github.io/ptnega-site&#8221;

Now, it’s easy to build your site to a specific directory. This directory contains all the file you need to push to your repository to run your blog.

At the top level of your working directory, use this command:

hugo

By default, your site will be built in the public/ directory. If you want to build it in a directory of your choice, use this command instead:

hugo -d your-build-directory

2. Create gh-pages branch in the repository

gh-pages is the branch of your repository which contains source codes from public/ in the previous step. Using git to create this branch. Note that this branch need to be orphaned

git checkout –orphan gh-pages

Now you’re switched to gh-pages branch. Simply copy all the file from public/ folder in the previous step to your working directory (which is now in gh-pages branch).

For your convenience, you can clean up the branch before copying neccessary files.

3. Push source codes to gh-pages branch

Now, everything is ready to be push to your repository.

# Add all the changes to commit

git add -A

# Commit the changes

git commit -m “Build gh-pages”

# Push everything to your repository

git push origin gh-pages

Now, your blog is online at, for example:

http://ducphanduyagentp.github.io/ptnega-site/

If you encounter any problem during the deploying process, feel free to leave a comment here and I will answer your question.

Leave a comment »

How to setup a blog using Hugo

Today I’m gonna show you how to create a simple blog using Hugo. Let’s go right into the tutorial!

1. Install Hugo

Go to Hugo Releases and download the appropriate version for your OS and architecture.

In this tutorial, I’m using Ubuntu 15.10. To install Hugo, I download hugo_0.15_amd64.deb and using the following command:

sudo dpkg -i path/to/deb-file

Note that, if you’re using Windows, you (may) need to add Hugo’s directory to PATH Environment Variable, so that you are able to use hugo in cmd just by typing hugo.exe [-options] [-parameters]

2. Create a new site

Now, you’ve got everything setup. Let’s create a new Hugo site using this command:

hugo new site path/to/site

For example:

hugo new site ~/ptnega-site

Now, your site is created in the ptnega-site directory. Navigate to that dir to continue working

cd ptnega-site/

3. Create some content

In the working directory, you will see the following file structure:

 

  • archetypes/
  • content/
  • data/
  • layouts/
  • static/
  • config.toml

There is something you need to know about these folders and file:

  • config.toml: The title of the blog, themes you use and other configuration is in this file
  • static/: The data in this folder will not be processed when you run the blog
  • content/: This is where your posts are, typically Markdown files

To create your very first post, just type the following command:

hugo new post/nameofyourpost.md

For example:

hugo new post/intro.md

Your new post is now in ptnega-site/content/post/intro.md

Open intro.md:

+++
Categories = [“Development”, “GoLang”]
Description = “”
Tags = [“Development”, “golang”]
date = “2015-12-26T22:50:04+07:00”
menu = “main”
title = “intro”

+++

  • Categories: You can put this post to any categories you like by adding the name of the categories to this array.
  • Description: You can briefly describe your post here.
  • Tags: Somehow similar to Categories
  • date: this is the time when your post was created
  • menu: This post will appear in the menu you set it to. “main” is the top most menu
  • title: The title of your blog. Change it to what you like.

Now, add your “actual content” to the bottom of this file. This is what you want to display in the post. For example:

+++
Categories = [“GCI”, “Google”, “GCI15”]
Description = “”
Tags = [“Self”, “GCI15”]
date = “2015-12-26T22:50:04+07:00”
menu = “main”
title = “Introduce myself”

+++

Hi! I’m ptnega from Vietnam. I’m 17 and this is my last GCI.

4. Install a theme

You’ve got your content. Now you need to display it on the site. Let’s install a theme for your site:

git clone –depth 1 –recursive https://github.com/SenjinDarashiva/hugo-uno.git themes/hugo-uno themes/hugo-uno

I’m installing hugo-uno theme. You can find many interesting ones here:

Hugo Themes

Now you have another folder named themes/hugo-uno. This is where your themes stored.

5. Config.toml

We’re almost done. Now let’s config this file to make sure everything will be in the right way.

A simple config.toml could look like this:

languageCode = “en-us”
title = “Ducphanduy’s Hugo Blog”
theme = “hugo-uno”

In this tutorial, I’m gonna run my site locally on my machine. There are other options when you install different themes or run the site on github-pages. A more complex configuration, for example:

baseurl = “http://ducphanduyagentp.github.io&#8221;
languageCode = “en-us”
title = “Ducphanduy’s Hugo Blog”
theme = “hugo-uno”

[Params]

AuthorName = “Duc Phan Duy”
github = “ducphanduyagentp”
twitter = “I_am_agentp_14”

baseurl is the URL to your site when you run it online. Providing a baseurl give you chances to use some features like Menu feature, …

[Params] is the parameters of the theme you installed. You will find the patterns for the params and other configuration of the theme is its folder above, in the README.MD file or the example site provided along with the theme.

6. Run the blog locally

To run the blog locally, simply use this command:

hugo server -w

By default, you site will be running on localhost:1313

Here is my result:

Screenshot from 2015-12-26 23-28-07

Hugo is extremely fast to run static blog and it provides users with many other convenient features. In my future posts, I will introduce some other features of Hugo that may be helpful to users.

You can have a look at my github repo of the example site in this post:

ptnega-site repo

Leave a comment »

Installing Wine on Ubuntu 15.10

In this post, I will show you different ways of installing Wine on Ubuntu, and in my tutorial, I’m using Ubuntu 15.10.

A. Installing in the simplest and most common way

  1. Using the command line

In Ubuntu, open a Terminal using the shortcut Ctrl + Alt + T then type the following command:

sudo apt-get install wine

You can also press the [Tab] key to see more options, like this:

Screenshot from 2015-12-25 23-54-13

You can change the “wine” parameter in the command above to “wine1.4” or “wine1.6”. It depends on which version of Wine you want to install.

By default, just leave it “wine” and hit [Enter]. You will then be asked to provide your password, just type it (it is not visible) and hit [Enter] one more time to start the installation.

Screenshot from 2015-12-26 00-48-46

2. Using the Ubuntu Software Center

Firstly, search for the Ubuntu Software Center

Screenshot from 2015-12-26 00-11-10

Then, search for Wine

Screenshot from 2015-12-26 00-12-01

Click on the Install button as shown in the picture above. Then, you’ll be asked to provide your password. Type in in the box and hit [Enter] to start the installation.

B. Installing using a PPA Repository

There are two main steps to install Wine in this way. I’ll guide you through them.

  1. Adding the PPA Repository
  • Using the command line

Open the Terminal and type the following command:

sudo add-apt-repository ppa:ubuntu-wine/ppa

You will be asked for your password and your confirmation of adding the repository. Type your password and hit [Enter] to continue.

Screenshot from 2015-12-26 01-25-48

After adding the repo, you need to update the APT package information:

sudo apt-get update

Screenshot from 2015-12-26 01-28-29

  • Using the User Interface

Search for the Software and UpdatesScreenshot from 2015-12-26 00-11-10

Navigate to the Other Sofwares tab:

Screenshot from 2015-12-26 00-13-05

Click on the Add.. button and then paste the line below to the box:

ppa:ubuntu-wine/ppa

Screenshot from 2015-12-26 00-12-55

Click Add Source and close the window. You will be prompted to update the APT package information.

Screenshot from 2015-12-26 00-13-09

Click Reload and… wait for it 😀

Screenshot from 2015-12-26 00-13-12

Now, you’ve done adding the PPA repository. Let’s move on to the next step.

2. Installing Wine using command line

Open a Terminal window and type the following command:

sudo apt-get install wine

This time, let’s press [Tab] to see if there’s any difference:

Screenshot from 2015-12-25 23-55-56

Notice the differences? We now have even more options than before. Here we’ve got the newest version – wine1.8. And this means that using a PPA Repository provides you the lastest version of Wine.

This time, I replaced “wine” in the command by “wine1.8”

I didn’t encountered any errors during the installation:

Screenshot from 2015-12-26 00-04-34

Screenshot from 2015-12-26 00-07-37

And done!

Screenshot from 2015-12-26 00-08-08

The first time you run Wine, you will be asked to install Wine-mono and Wine-gecko. Just click Install and everything will be okay.

Screenshot from 2015-12-26 00-09-00

Hope you find my tutorial useful. If you encounter any problems during the installation, let me know in the comments.

Leave a comment »

My first task in GCI 2015

Hello everyone!

It’s been a long time since I last posted on my blog. I’m so excited because I’ve just completed my very first task in GCI 2015 and appeared on FOSSASIA GCI15 site.

Here is my pull request:

Github pull request

I’ll do my best in GCI15. Thank you so much.

Leave a comment »

How to set up fossasia.github.io locally using Jekyll

In this post, I’ll show you how to set up fossasia.github.io locally using Jekyll. This post is for this Google Code-in 2014 task from FOSSASIA:

https://www.google-melange.com/gci/task/view/google/gci2014/5794409786376192

So, these are what you need to run this site locally on your machine:

1. Jekyll data files of the site. You can find it in its github repository.

2. Jekyll, of course 🙂

Let’s start!

1. How to get Jekyll data files of the site?

There are two ways to get the repository on your computer.

a. You can download a zip file contains all the files you need:

Screenshot from 2015-01-11 11:49:14

Extract the zip file and you will have the data you need.

b. You can also clone the repository from github:

You can fork the repo or not. It’s optional. To fork the repo, click the button on the top-right corner of the page:

Screenshot from 2015-01-11 11:49:14

Here, I didn’t fork the repo to my account.I cloned it directly from fossasia using this command:

git clone https://github.com/fossasia/fossasia.github.io

Screenshot from 2015-01-11 11:39:07

It will be easy to sync my local repo on the machine with the one from fossasia. If you clone the forked one, use this command instead:

git clone https://github.com/yourusername/fossasia.github.io

To get updates from the owner of the repo, follow the instruction here: https://help.github.com/articles/syncing-a-fork/

Use this command to get into your workspace:

cd fossasia.github.io

You had your data files. Let’s setup Jekyll!

2. How to setup Jekyll?

To have Jekyll installed on your machine, you need some requirements:

– Ruby >= 1.9.2 (I encountered problem with Ruby’s version and found this requirement. I haven’t read the instruction very much, just installed Jekyll my myself)

– Rubygems, you had “gem” when you installed Ruby 😀

– NodeJS, or another JavaScript runtime (for CoffeeScript support)

Last but not least: Linux, or Mac OS X. I’m using Ubuntu 14.04 LTS.

Here’s how I installed Jekyll:

a. Installing ruby and get ‘gem’

The first time I read the task, I made a mistake, silly mistake. I “installed Jekyll using apt-get” (You know, “sudo apt-get install jekyll”, can’t help laughing at myself). In this task, Jekyll can’t be installed using that command. You should install Jekyll using rubygems. So, firstly, I installed ruby using this command:

sudo apt-get install ruby1.9.3

Screenshot from 2015-01-10 23:35:28

Actually, I had installed jekyll before working on this task. So I had to remove all the requirements, and uninstall jekyll to get those screenshots 😀 Absent-minded… As I said, I had problem with ruby when install Jekyll using ‘gem’. You need ruby with version >= 1.9.2 to go through the installation. I’m sorry for not having the screenshot of that error here.

I don’t know if other users meet this problem. You can leave comment here to make this problem clear 😀

I check my ruby version using this command:

ruby -v

Screenshot from 2015-01-10 23:35:41

Ah, now I have ruby1.9.3 installed 😀 both ‘gem’ and ruby 😀

Let’s get to the next step.

b. Installing JavaScript Runtime

I use NodeJS as JavaScript runtime. A very familiar command to us:

sudo apt-get install nodejs

Screenshot from 2015-01-10 23:36:39

I had no trouble in installing nodejs.

Now I had all the requirements. The final step to install jekyll, is “using gem to install Jekyll” 😀

The first time I use gem to install Jekyll, I met this message:

ERROR: While executing gem … (Gem::FilePermissionError)
You don’t have write permissions into the /var/lib/gems/1.9.1 directory.

Screenshot from 2015-01-11 13:02:19

It’s because I didn’t run this command as root. So add “sudo” to the beginning of the command to solve this problem 🙂

Another look at that error. As I said before, I met problem with ruby version but don’t have screenshot here. I found it’s strange that the command requires permissions for /var/lib/gems/1.9.1 directory, but I get error for not having ruby1.9.3 (!?) I think I need some more comments from other users.

This time I run the command without geting any errors:

Screenshot from 2015-01-10 23:53:01

Now you had Jekyll installed on your computer 🙂

How I run fossasia.github.io locally on my machine?

– Go into the fossasia.github.io folder you cloned or extracted in the 1st step.

– Simply run this command:

jekyll serve

Screenshot from 2015-01-11 13:28:47

Then I opened my browser: localhost:4000. Here the result:

Screenshot from 2015-01-10 23:55:14

Screenshot from 2015-01-10 23:55:21

Screenshot from 2015-01-10 23:55:28

Screenshot from 2015-01-10 23:55:43

Screenshot from 2015-01-10 23:55:52

“IT’S WORKING, YAYYYYYY…”

I was so excited then… Because the biggest problem I faced through the installation, is the speed of Internet. I’m from Vietnam, and the AAG (Asia-America Gateway) cable, which is the main cable of our region, has broken since 5/1/2015. It’s being fixed, and hasn’t done yet. I had to keep my machine on for a long time just to install the requirements, or download something, or just google something. Every activity takes time because of slow Internet 😦

I also met difficulties about “dependencies” while using apt-get. But it didn’t last long. I just used some familiar command such as: “sudo apt-get -f install”, …

Sometimes, when I had to wait during the installation, I played the guitar. It’s interesting and relaxing. You may want to try 😀

Moving to Jekyll has advantages. These are what I think:

– It’s simple to run the website with HTML files.

– No database, just static parts loaded. So, more security.

– Easier to contribute to the website. You can make changes on your own repo, run for a test, and ask the owner of the site for changes.

– Using markdown, so easy to change things.

Hope you enjoy my “experience”. Thanks for reading 🙂

And… sorry for my bad English.

Leave a comment »