Online Python IDE | Google Colaboratory cloud IDE introduced

1. Recommended Reading

Basics article on Python | Python2 and Python3 and VSCode download and install, PyCharm crack installation, Python online IDE, Python video tutorial: https://blog.csdn.net/COCO56/article/details/95542228

2. Press

Colaboratory is a Google provided Jupyter notebook environment, does not require any settings you can use, run entirely in the cloud, do not take local resources, in addition Colaboratory free.

3. Using articles

  1. Preparation phase
    Colaboratory is a Google service, I recommend using the chrome browser to access Google + helper, so that when access Google related sites is very convenient. In addition, Colaboratory normal use need to rely on Google account, if not a recommended registration.
    After the above work is completed, you can visit the website Colaboratory to experience something, and this figure is to introduce the following page Colaboratory (hereinafter referred to as colab) of.
    Official website address: https://colab.research.google.com/
    Here Insert Picture Description
  2. 基本用法
    用过jupyter notebook的朋友应该对这个界面非常的熟悉,事实上colab的操作与普通的notebook相似度非常高,下面我们就新建一个Python3的notebook(下图),简单地演示一下它的基本用法。
    点击“新建PYTHON3 记事本”
    Here Insert Picture Description在下图中的代码框中输入Python代码,点击左侧的按钮执行程序,就会在下方输出打印结果,由于程序第一次运行之前colab会自动连接云端服务器,所以速度可能会有点慢。
    Here Insert Picture Description
    值得一提的是,上面这个代码框可不是只能执行Python代码,你还可以把它当作ubuntu的终端来使用,只不过要在所有需要执行的指令之前加一个“!”号。例如,我们如果想查看当前的云端服务器中自带了哪些Python库的话,可以执行“! pip list”指令,结果如下:
    Here Insert Picture Description
    可以看到,colab默认安装了大量的Python第三方库,就数据科学方面的库而言,colab的自带库应该会比Anaconda更加完善。
    既然可以使用pip指令,那如果想安装其他Python库的时候也可以很方便地安装了,我拿tushare做了一个试验,结果如下:
    Here Insert Picture Description
    除了python库之外,使用这种方式还可以执行其他linux指令来部署你自己的云端环境,别忘了在指令前加一个“!”号就好。

    4. 配置篇

  3. 常用配置
    我个人认为最常用的配置项主要集中在两个地方:
    一是"工具"中的偏好设置(下图),在这里可以设置主题背景(共分light和dark两种)、缩进宽度等风格,有趣的是colab还自带coding特效,如果威力等级一栏选择了“many power”,那么在写代码的过程中就会产生非常炫酷的效果。
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    二是“修改”中的笔记本设置,这里可以设置运行时的python版本和硬件加速器。说到这里不得不佩服谷歌,居然提供了GPU和CPU两种加速模式,只要在选项中进行设置,就可以免费获取额外的算力,这下通过在线编辑器使用TensorFlow再也不是梦想了。
    Here Insert Picture Description
    Here Insert Picture Description
    除此之外,colab还允许通过共享的方式邀请他人共同讨论、完善代码;如果你不想将代码放到云端服务器中运行,还可以连接本地的开发环境运行程序(下图)。
    也可以将colab与github相关联,把notebook中的代码备份到GitHub中...关于这些内容,本文不进行详细说明,有兴趣的小伙伴可以自行尝试。
    Here Insert Picture Description
  4. 云盘挂载
    先来说说挂载云端硬盘的重要性。colab中的代码文件是放在云端服务器中运行的,但存储的位置却是同一账户的谷歌云端硬盘,如果不进行关联设置,colab会默认将notebook文件放到云端硬盘根目录下的“Colab Notebooks”文件夹中。
    谷歌云盘官网:https://drive.google.com/drive/my-drive
    Here Insert Picture Description
    colab的notebook在云端服务器中运行期间是没办法直接读取本地文件的(比如数据集),如果想让程序读取指定文件,只能将其放到谷歌云端硬盘中,然后将云端硬盘挂载到colab。
    如果在colab中保存过notebook文件,系统将会自动关联你的colab和云端硬盘账户,否则需要在云端硬盘中进行手动关联(此处略)。然后在代码框中输入以下代码并运行。
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

Here Insert Picture Description
运行过程中会出现下图中的提示,点击链接进行授权验证,将授权码输入到链接下的文本框中,按回车键继续执行。
注意:不同的环境在这一步的执行情况可能会不太一样,需要灵活对待。
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
接着再执行下面两行代码,由于我在挂载前云端硬盘中有文件,所以加上了nonempty参数,否则可以忽视。

!mkdir -p drive
!google-drive-ocamlfuse -o nonempty drive

顺利的话,到这里云盘挂载就完成了,默认挂载的云盘根目录路径是’drive‘,我们来检验一下(下图),可以看到输出的内容和云端硬盘中的文件是一致的。
Here Insert Picture Description

import os
print(os.getcwd())
print(os.listdir('.'))
print(os.listdir('drive/Colab Notebooks'))

Here Insert Picture Description
这样一来,我们就可以将文件放到云盘中供colab读取,或者将colab的运行结果输入到云盘中了。

5. 教学篇

colab as an online editor, through cloud computing let us get rid of the limitations of equipment, no matter what the device, as long as connected to the Google Web service, you can use the cloud server, processing cloud data set; at the same time, for the workplace people who are not fixed, but also eliminates the trouble of repeatedly copying files and configuration environment.
However, colab to their own position is designed to help spread the training of machine learning and research, so Colab also associated with a very good learning machine learning platform.
Here Insert Picture Description
Click on the link above the "full course website" to enter the teaching website, where not only have an excellent learning materials, but also according to each person's different basis for the development of learning programs, more unusual is that, regardless of video, voice or text data are Chinese mode can be selected (although suspected Chinese reading speech synthesis).
Here Insert Picture Description
In addition to teaching website, as well as end-to-colab example (seedbank) analysis of a large number of interactive machine learning for learning and practice, all seedbank in the project can be a key import colab run (figure below).
Here Insert Picture Description
About Colaboratory briefly here today, obviously it is the section editor, but integrated teaching function, is really called the strongest in the history. Due to limited space, some of the details and functions are not introduced, interested students can explore the experience.

Guess you like

Origin www.cnblogs.com/coco56/p/11205816.html