How to run Python code on iPad?

How to run Python code on iPad?

In fact, it’s not just iPads, mobile phones can also be used.

Pain Points

I have organized several offline programming workshops, leading students to use Python to solve data science problems.

The most troublesome thing is the installation of the operating environment.

[[229542]]

To be honest, I had made serious preparations before attending the workshop.

For example, for the integrated environment, we chose the user-friendly Anaconda.

The code ran on my Macbook computer without any problems. I also ran it on my student's Windows 7 computer without any problems. Then I uploaded it to Github.

In the published tutorial article, I have also written very detailed instructions for installing the software package.

A video was also recorded specifically for the installation and operation of Anaconda, a Python runtime environment.

However, the problems encountered at the workshop are still varied.

Some are operating systems. For example, you may use Windows 10. To be honest, I have never used it. I looked at the Surface carefully and couldn't even find the Anaconda folder after installation.

Some are encodings. Different operating systems have default Chinese encodings of UTF-8 and GBK. The same Chinese text may appear normal on my screen but garbled on yours.

Some are package paths. Before coming to the workshop, you may have watched some of my tutorials and installed Anaconda for Python 2.7. When you arrived, you saw that you needed Python 3.6, so you installed a new one. As a result, when you run it, you can't tell which package the Python and pip commands you run come from, and you can't figure out where the software packages are installed. Add to that the virtual environment configuration, and you'll go crazy.

Others even face network congestion issues, as sometimes large software packages need to be installed on-site, and dozens of computers are "ready-to-go" to compete for limited Wifi bandwidth, with predictable consequences.

After learning from my mistakes, I decided to change the status quo.

The current tutorial only provides basic source code, which is not enough for many novice students.

Many students failed on the road of installing dependent software packages and then simply gave up.

There are many workarounds, such as recording a video of the code execution for you to watch.

But as I said in the article "What is the most important thing in MOOC teaching?", feedback is the most important thing in the learning process.

You need to be able to run your code and get immediate feedback on the results.

On this basis, you must also be able to modify the code and compare the execution results before and after.

I have to provide you with an environment where you can run it directly.

With zero installation, there is naturally no need for the above troubles.

Is this possible?

I researched it and it's ok.

All you need is a modern browser on your device (including but not limited to Google Chrome, Firefox, Safari, and Microsoft Edge, etc.).

IE 8.0?

That won't work, upgrade now!

After reading this, you should have figured it out. Because it only selects the browser, not the operating system, so let alone Windows 10, you can run the code even if you are using an iPad.

try

Please open your browser and enter this link (http://t.cn/R35fElv).

See what happens?

I'll demonstrate this to you using an iPad.

At first, a startup interface will appear. Please wait for about 10 seconds.

Then, you can see the familiar Python code running interface.

This interface comes from Jupyter Lab.

You can think of it as an enhanced version of Jupyter Notebook, which has the following features:

  • The code unit can be directly dragged with the mouse;
  • One browser tab can open multiple Notebooks, and each uses a different Kernel;
  • Provides a Markdown editor with real-time rendering;
  • Full file browser;
  • A quick tour of CSV data files

The left column in the figure shows all the files in the working directory.

The file opened on the right is the ipynb file we are going to use.

To prove that I am not kidding, please click the Run button in the toolbar above the code on the right.

Click it and the result of the current code unit will be run.

Keep clicking and you can see that the results are rendered normally.

Even the images are displayed normally.

Even the following visualization results that require a certain amount of calculation are no problem.

To prove that this is not magic, you can write a line of output statements in a new cell.

Just let Python output your name.

If your name is Chuck, you would write:

  1. print( "Hello, Chuck!" )

Replace it with your own name and see if the output is correct?

In fact, it’s more than just the iPad.

If you are brave enough to try, mobile phones can also be used.

Just like this.

process

Now let me tell you how this effect is achieved.

We need to use a tool called mybinder. It can help us quickly convert a code repository (repo) on GitHub into a runnable environment.

Note that mybinder provides us with cloud facilities, that is, computing resources and storage resources. Therefore, even if many users use the same code-converted environment online at the same time, there will be no conflict.

Let's first take a look at how to prepare a code repository that can be smoothly converted by mybinder.

The sample I provide for you is here (http://t.cn/R35MEqk):

By the way, this example comes from my data science series tutorial "How to process natural language with Python? (Spacy and Word Embedding)". Interested students can click the link to view the original text.

In the list of files shown on the GitHub page, you need to pay attention to the following three files:

  • demo.ipynb
  • environment.yml
  • postBuild

demo.ipynb is the Jupyter Notebook file containing the source code you saw in the previous section. You need to first install the relevant packages locally and run the test.

If there are errors when you run it locally, it will probably be difficult to run it properly when you put it on the cloud.

The environment.yml file is very important. It tells mybinder how to prepare the environment for your code to run.

Let's open the file and take a look at its contents:

  1. dependencies:
  2. - python=3
  3. - pip:
  4. - spacy
  5. - ipykernel
  6. - scipy
  7. - numpy
  8. - scikit-learn
  9. - matplotlib
  10. - pandas
  11. -thinc

This file first tells mybinder your Python version. We use version 3.6, so just specify python=3. mybinder will automatically download and install the latest version for you.

Then this file describes which packages need to be installed using the pip tool. We need to list all the dependent installation packages.

These are the preparation steps that I always explained to you in the tutorials before.

But that's not all, because mybinder just installs some software dependencies for you.

There are two more steps to take care of here:

  • In order to analyze semantics, we need to call the pre-trained Word2vec model, which requires mybinder to download it for us in advance.
  • When Jupyter Notebook is opened, the kernel name that should be used is wangshuyi, which is not registered in Jupyter yet. We need mybinder to do it for us.

To complete the above two steps, you need to prepare a postBuild file.

Its contents are as follows:

  1. python -m spacy download en
  2. python -m spacy download en_core_web_lg
  3. python -m ipykernel install --user --name=wangshuyi  

Just like its name. It is the command that mybinder executes in sequence after installing the dependent components according to environment.yml. If your code needs other commands to provide environment support, you can also put them here.

At this point, your preparations are complete.

The magic show officially begins.

Please open the mybinder website (https://mybinder.org/).

In the "GitHub repo or URL" field, fill in our GitHub code repository link, that is:

  • https://github.com/wshuyi/demo-spacy-text-processing

We hope that demo.ipynb will be opened automatically as soon as we enter the interface, so we need to fill in demo.ipynb in the "Path to a notebook file (optional)" column.

At this point, you will find the URL of your code runtime environment in the "Copy the URL below and share your Binder with others:" column.

  • https://mybinder.org/v2/gh/wshuyi/demo-spacy-text-processing/master?filepath=demo.ipynb

Click the "Copy" button on the right to save it in your notepad. You will rely on it to find your converted operating environment in the future.

Once you have the address saved properly, click the “Launch” button.

The time you need to wait may vary depending on the number of dependent installation packages and other factors. But it will take some time only when you build for the first time.

Every subsequent call will be very fast.

After the build is complete, mybinder will automatically open the corresponding operating environment for us.

It's quite a sense of accomplishment!

Test it and if the code can run normally, it proves that we are successful.

But you will find that it is not true!

Teacher, what you just demonstrated on iPad, wasn't it the advanced version of Jupyter Lab? How come it became Jupyter Notebook?

I want the premium version too!

Don't be in a hurry.

Check your current link address:

https://mybinder.org/v2/gh/wshuyi/demo-spacy-text-processing/master?filepath=demo.ipynb

You just need to make a small adjustment:

  1. ?filepath=

Replace with:

  1. ?urlpath=lab/tree/

The replaced link is:

https://mybinder.org/v2/gh/wshuyi/demo-spacy-text-processing/master?urlpath=lab/tree/demo.ipynb

Type this into your browser and see what comes out:

Is this all right now?

principle

Do you think mybinder is very high-tech?

Actually, not really.

It just links together several existing technologies.

This can probably be considered an example of "building block innovation".

Let's look at the description of mybinder:

As you can see, the most critical technology is the use of Docker.

What is Docker?

Simply put, Docker is a tool that ensures that the same code can be successfully executed on different platforms.

You are a little hesitant. Isn't this talking about Java?

That’s right, Java’s slogan is to code once and run everywhere.

It uses virtual machines to ensure this capability.

However, if you often use tools developed in Java, you should understand the pain points.

At least, you should have some experience with the running speed of Java programs.


In the picture above, the virtual machine is on the left and Docker is on the right.

Not only is Docker more efficient than the Java virtual machine, but it also supports more than one programming language.

As for other benefits, we won’t go into details, otherwise it will sound like an advertisement.

In fact, the work of converting the GitHub code repository into a Docker image is not done by mybinder itself.

It calls another tool called repo2docker (https://github.com/jupyter/repo2docker).

Your browser can execute Python code because Jupyter Notebook (or Lab) is built on the "Browser / Server" (B/S) structure.

If you have already installed Anaconda on your local computer, you can try executing this statement locally:

  1. jupyter lab

What will happen?

Yes, it starts a server and then opens your browser to communicate with the server.

The design of Jupyter makes it extremely convenient to expand.

Whether the Jupyter server is running on your local laptop or in a server room on another continent, it makes no difference to the way you execute Python code.

In addition, if you think mybinder can only allow you to run Python code in the browser, then you underestimate it.

For those who have learned R, please click this link (http://t.cn/R3JLY2S) to see what surprises there are.

summary

In summary, this article tells you the following:

  • How to use mybinder to convert a github repo into a Jupyter Lab runtime environment with one click;
  • How to run the environment on browsers of various operating systems to write, execute and modify code;
  • mybinder converts github repo behind the scenes hero docker introduction.

I hope you can think of more uses than just these simple ones.

Here are a few questions for you to think about:

  • If code execution is all done in the cloud, is it necessary to pre-install a lot of software in the teaching laboratory computer room and update and maintain it from time to time?
  • Is it possible for school programming exercises, homework, and exams to be conducted remotely and graded automatically in this way?
  • Since the applied technologies are all open source, is it possible for you to use these open source tools to start a startup project, such as providing a deep learning environment and leasing it to scientific research institutions and startups?

I hope you can draw inferences from this and make interesting and meaningful innovations.

<<:  8 excellent Android PDF reading apps for viewing documents

>>:  HTC launches blockchain phone, is it the last attempt to subvert Or?

Recommend

9 Strategies for Optimizing E-Commerce Product Details Pages

Consider the following scenario: Let’s say you ar...

Is 360 information flow advertising effective for men?

As a search engine media advertising platform, 36...

Family members, helmets really can save lives! It’s not just talk!

When driving on the road, safety is the most impo...

APP operation: What factors lead to serious loss of APP users?

During the operation of a product, there is one m...

Dong Mingzhu: If we hadn't offended those people, Gree wouldn't be where it is today

  Dong Mingzhu’s famous quotes and stories are alw...

Where to open the Kuaishou shopping cart?

This article mainly introduces the relevant infor...

SEM bidding hosting service content

The content of SEM bidding hosting provided by Ku...

11 ways to attract private domain traffic on Douyin

Although Douyin is relatively more focused on pub...

How can Douyin enter the second wave of traffic pool? What are the methods?

On Douyin , each short video enters the Douyin tr...