Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to run Python code with iPad

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use iPad to run Python code, the content is very detailed, interested friends can refer to, hope to be helpful to you. In fact, not only iPad, but also mobile phones can do it.

Pain point

I have organized several offline programming workshops, taking students to deal with data science problems with Python.

One of the biggest headaches is the installation of the running environment.

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

For example, the integrated environment chooses Anaconda, which is very user-friendly.

The code runs on my Macbook computer, no problem. Also get the students' Windows 7 to run, no problem. It was uploaded to Github.

In the released tutorial articles, I have also written very detailed instructions for installing the package.

Video has also been specially recorded for the installation and operation of Anaconda, a Python operating environment.

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

There are plenty of operating systems. For example, you might use Windows 10. To be honest, I haven't used it. Take a look at the Surface, and you can't even find the installed Anaconda folder.

There's plenty of code. Different operating systems, some default Chinese code is UTF-8, some are GBK. The same paragraph of Chinese text, I show that everything is normal here, where you are garbled.

There are plenty of package paths. Before coming to the workshop, you may have read some of my tutorials and installed Python version 2.7 Anaconda. When you come to the scene, as soon as you see that Python version 3.6 is required, you have installed a new one. As a result, you can't tell which package the Python and pip commands are running from, let alone where the package is installed. Coupled with the virtual environment configuration, you are going to freak out.

There is even the problem of network congestion. Because it is sometimes necessary to install and call large software packages on site, dozens of computers are "ready-together" to compete for limited Wifi bandwidth, and the consequences can be imagined.

Learning from the bitter experience, I decided to change the status quo.

The current tutorial only provides the basic source code. For many novice students, it is not enough.

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

There are many ways to adapt. For example, simply record a code execution video to show you.

But as I said in the article "MOOC Teaching, what is the most important?", feedback is the most important in the learning process.

You need to be able to run the code and get feedback on the results in * time.

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

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

Zero installation, of course, without the above troubles.

Is this possible?

I studied it, no problem.

As long as you have a modern browser on your device (including but not limited to Google Chrome, Firefox, Safari, Microsoft Edge, etc.).

IE 8.0?

I can't do that. Upgrade now!

At this point, you should have figured it out. Because you only pick browsers, not operating systems, so don't say you use Windows 10, even if you use iPad, you can run code.

Try

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

See what happens?

I'll show you with iPad here.

There will be a startup interface at first. Please wait for more than 10 seconds.

Then you can see the familiar Python code runtime interface.

This interface is from Jupyter Lab.

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

Code unit direct mouse drag

A browser tag that can open multiple Notebook and use different Kernel

Markdown editor that provides real-time rendering

Complete file browser

Quick browsing of CSV data files

……

The left column in the picture is all the files under the working directory.

What opens on the right is the ipynb file we want to use.

To prove that this is not a joke, please click the run button on the toolbar above the code on the right.

One click will run the result of the current code unit.

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

Even the image can be displayed normally.

Even the following visualization results, which require a certain amount of computation, are fine.

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

Just let Python output your name.

If your name is Chuck, write like this:

Print ("Hello, Chuck!")

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

As a matter of fact, it is not just iPad.

If you are brave enough to xin (bing) and try (kuang), a mobile phone will actually work.

Like this.

Process flow

Let me tell you how this effect is made.

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

Note that mybinder provides us with cloud facilities, that is, computing and storage resources. So even if a large number of users use the same code conversion environment online at the same time, they will not conflict with each other.

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

The example I provided to you is here (http://t.cn/R35MEqk):

By the way, this example comes from my series of tutorials on data science, "how to use Python to deal with Natural languages? (Spacy and Word Embedding)." Interested students can click on the link to view the original text.

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

Demo.ipynb

Environment.yml

PostBuild

Where demo.ipynb is the Jupyter Notebook file that you saw in the previous section that contains the source code. You need to first install the relevant software package locally and run the test to pass.

If there are errors in your local operation, it must be difficult to run properly if you put it on the cloud.

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

Let's open it and look at the contents of the file:

Dependencies:-python=3-pip:-spacy-ipykernel-scipy-numpy-scikit-learn-matplotlib-pandas-thinc

This file first tells mybinder your Python version. We use version 3.6. So you just need to specify python=3. Mybinder will automatically download and install * * for you.

This file then indicates which packages need to be installed using the pip tool. We need to list all dependent installation packages.

These are the preparation steps I always showed you in the tutorials.

But this is not over, because mybinder has just installed some software dependencies for you.

There are two more steps to deal with:

In order to analyze the semantics, we need to call the pre-trained Word2vec model, which requires mybinder to download it for us in advance.

After the Jupyter Notebook is opened, the name of the kernel you should use is wangshuyi, and this kernel is not currently registered in Jupyter. We need mybinder to do it.

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

Its contents are as follows:

Python-m spacy download en python-m spacy download en_core_web_lg python-m ipykernel install-user-name=wangshuyi

It's the same as its name. It is a command that executes sequentially after mybinder installs the dependency build according to environment.yml. If your code needs other commands to provide environmental support, you can also put it here.

At this point, your preparatory work is over.

The magic show officially begins.

Please open the website of mybinder at https://mybinder.org/).

In the "GitHub repo or URL" column, fill in our link to our github code repository, namely:

Https://github.com/wshuyi/demo-spacy-text-processing

We want to open demo.ipynb 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 that the URL of your code runtime appears 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 to your notepad. It all depends on it to find a good operating environment for you in the future.

After properly saving the address, click the "Launch" button.

Depending on factors such as the number of packages you rely on, the amount of time you need to wait varies. But only * builds, and it takes some time.

In the future, every call will be executed very quickly.

After the build is completed, mybinder will automatically open the corresponding running environment for us.

Have a great sense of achievement!

Testing and being able to run the code properly proves that we are successful.

But you will find that it is not right!

Teacher, what you just showed in iPad, isn't it the advanced version of Jupyter Lab? Why is it Jupyter Notebook again?

I want the premium version, too!

Take it easy.

Look at 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 and put one of them:

? filepath=

To be replaced by:

? urlpath=lab/tree/

The replaced link is:

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

Type it into the browser and see the result:

Is that all right?

Principle

Do you think mybinder is cool techs?

Actually, not really.

It just links several existing technologies together.

This is probably an example of "building block innovation".

Let's take a look at mybinder's instructions:

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

What is Docker?

To put it simply, Docker is a tool to ensure the smooth execution of the same code on different platforms.

You're a little hesitant, aren't you talking about Java?

Yes, the slogan of Java is to code and run everywhere.

It uses virtual machines to guarantee this capability.

However, if you often use tools developed by Java, you should know what the pain points are.

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

In the figure above, there is a virtual machine on the left and Docker on the right.

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

As for other benefits, we will not expand. Otherwise, it sounds like an advertisement.

In fact, mybinder does not do the work of converting github code repositories into docker image.

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

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

If you have already installed Anaconda on your local computer, take a look at the local execution of this statement:

Jupyter lab

What's going to happen?

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

This design of Jupyter itself makes it extremely convenient to expand.

Whether the Jupyter server is running on your local laptop or in a computer room on another continent, there is no essential difference for you to execute Python code.

In addition, if you think that mybinder can only let you run Python code on your browser, you are underestimating it.

On how to use iPad to run Python code to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report