How to import jupyter notebook to another jupyter notebook? - python

So I'm trying to make a series of educational coding using jupyter. I wanted to create segmented parts by parts for exp. part1.ipynb, part2.ipynb, and so forth.
so when i use
from partuno import *
It returns an error and does not read the previous part even though they share the same folder location in jupyter. My question is are there any other way for me to import them into the next part?

You need to use the "import_ipynb" library of python.
if you want to use the contents of one.pynb in two.pynb then
import import_ipynb
import one.pynb
now you can use the functions defined in one.pynb in you two.pynb

Step 1.
pip import_ipynb
Step 2.
import your file at jupyter notebook.
Step 3.
import my_math.ipynb file at test.ipynb.

Related

Synapse Notebook reference - Call Synapse notebook from another with parameters

I have a synapse notebook with parameters .I am trying to call that notebook from another notebook. I am using %run command.
How should I pass the parameters from the base notebook to the one that is being called?
Also, for me the above answers didn't work.
As a separate solution to this, below is an answer.
Open the notebook and go to the properties tab on extreme right adjacent to 3 dots.
Check "Enable Unpublish Notebook reference."
Commit all changes.
Now, you will be able to use both %run and mssparkutils command.
at this time you should first import an library
from notebookutils import mssparkutils
mssparkutils.notebook.run('yourfolder/yournotebook')
Can you use Python and follow the example shown here?
mssparkutils.notebook.run("folder/Sample1", 90, {"input": 20 })

Failing import of zipped library with py-files

I have to maintain an oll code running with pyspark.
It's using a method I've never seen.
I have some reusable code zipped into a file ingestion.zip.
Then, this file is called using a pipeline.cfg file like this:
[spark]
master=spark://master
py-files=${HOME}/lib/ingestion.zip
spark-submit=${SPARK_HOME}/bin/spark-submit
When I'm trying to import the library as shown below, I cant make Pycharm understand that the lib should point to the zip file.
from ingestion.data import csv, storage
I've seen the zip is a solution proposed by spark-submit using py-files but how can I make it running on my IDE ?
I haven't used below method with pycharm, but it worked for us with spark-submit and we could import these modules using normal import statements.
Actually, we had a very few files to import and we needed something quick. So, if you also have the same use-case and if pycharm allows then maybe, you can give it a try.
--py-files s3://bucket-name/module1.py,s3://bucket-name/module2.py,s3://bucket-name/module3.py,s3://bucket-name/module4.py"
(Note - there shouldn't be any spaces.)
(Note - this suggestion is only an interim solution till someone replies with a better answer.)

CPU and RAM needed to run Python code on Azure

I'm trying to recreate on of the charts found at the RtCOVID-19 website, using a previously archived version of the code found here. I am using Spyder via Anaconda to run the Python scripts.
After cloning the repo, I create a project and attempt to run the following lines of code, pulled from this jupyter notebook in the repo, which should output the tables needed to create the 'Oregon' graph.
import pymc3 as pm
import pandas as pd
import numpy as np
import arviz as az
from matplotlib import pyplot as plt
from covid.models.generative import GenerativeModel
from covid.data import summarize_inference_data
from covid.data import get_and_process_covidtracking_data, summarize_inference_data
df = get_and_process_covidtracking_data(run_date=pd.Timestamp.today()-pd.Timedelta(days=1))
region = "OR"
model_data = df.loc[region]
gm = GenerativeModel(region, model_data)
gm.sample()
To see an example of the desired output, use the link to the Jupyter notebook referenced above.
The issue that I am running into is that my computer is not powerful enough to run the NUTS sampler. Whereas in the Jupyter notebook, we see that the authors are able to run the sample in 7m, my computer gives me an estimated run time of 4h and gets incredibly hot in the process. As such, I simply stop the model from running lest my computer explode into flames.
Some IT folks that I know said that they can create an instance in Azure to run these scripts, which would give me significantly more computing power, but they need to know how much CPU and RAM I need. Can anybody help me out with this? I only need to run the model one time, for example to recreate the Oregon chart, rather than all 50 charts as shown on the website. More generally, is the solution to this problem indeed to run the model in a cloud computing environment/is this possible?

Setting import function to simplify the import process in Python

Here is my question.
I use ipython notebook for daily data processing and analysis. When I create a new notebook, some essential packages must be imported first. After long-time accumulation, some process are interlinked and oriented to different task.
I can summarize the nature of my common project into these classes:
Data processing(numpy,scipy,etc. eg, from scipy import interpolate)
Data tiding(Pandas, csv, etc)
Dealing with scientific format data(netcdf4,pyhdf.eg: from osgeo import gdal)
Basic plotting(Matplotlib,Pylab)
Plotting attribute adjust. eg:
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.tri import Triangulation, UniformTriRefiner
from matplotlib.collections import PatchCollection
...
I often meet different tasks with similar working processes. The packages and some user defined function are the same(For example, I often write the correlation function myself for faster speed than pandas.corr). I had to search for past notebooks to find relevant code and copy them. Sometimes, I forget where to find them but I always know my working pattern.
So, my question begins
Is it possible to generate an meta-function library which will represent these features:
When I first work out some problem, I'll devise it into a pervasive function with broad import (one simple case, one user defined colormap can be stored for use another day)
When I use an brand new notebook, I don't need to reproduce the import process (for now, I had to write 41 lines dealing with regular work, and some of them are not essential for this project). I just need to think about every working pattern I had created and import them easily!
For example: Looping and reading specific lines in .csv file can be reproduced easily.
If it's possible, the notebook can be neat and clear!
It is certainly possible. You'll just need to understand the Python import rules- any package that appears as a sub-directory of your PYTHONPATH environment variable can be imported with a traditional import statement.
So, say you had a directory called ~/python/mypatterns containing your set of utility code. You would add ~/python to your PYTHONPATH, and ensure that there is a file called init.py (it doesn't need any contents) in that directory. So...
# Setup your environment
touch ~/mypatterns/__init__.py
export PYTHONPATH=${PYTHONPATH}:~/python
# command to start your notebook...
# in your notebook...
from mypatterns.myfile import myfunction
...

Permanently add Libraries in Python

I have this simple piece of code in a file:
frame = DataFrame(np.random.randn(2, 4),
index=pd.date_range('1/1/2000', periods=2, freq='W-WED'),
columns=['Colorado', 'Texas', 'New York', 'Ohio'])
When I try to run the file I get error messages telling me that it doesn't know what to do with DataFrame, np, or pd. I can fix this easily by adding in "
from pandas import DataFrame
import numpy as np
import pandas as pd
My question: is there a way that I can avoid having to import these libraries within every file that I want to use those tools?
I had imported them in the command line before running this file but this didn't seem to make a difference.
New to Python. Thanks in advance for the help.
Using Python 2 in the Canopy editor.
Generally, if you are going to use a package/module, then you should* import it in every module that needs it. While it might be a little annoying writing the same imports over and over, it's better in the long run because it makes your code a lot more clear. e.g. you know what np or DataFrame is and where it comes from.
I suppose that it's worth noting that you probably can get around this requirement by writing your own import hook, but . . .That's way too complicated a thing to do just to enable you to write code that will likely confuse your co-workers :-)
*There are some conveniences that you can use to get around this in the interactive interpretter, but not in anything that gets imported via the normal mechanisms.

Categories