Is it possible to create a variable in a code cell on Jupyter (running Python) and call this variable in a markdown cell? I want to put the variable into a LaTex expression.
You could format the output of a function with IPython.display.Markdown.
Related
Output format without print function
If I run a JupyterNotebook cell with
dataframe.describe()
a pretty fromatted table will be printed like that:
VSCode JupyterNotebook dataframe.describe() solo cell printing format
Output format with print function
If I run a cell with more than just one line code dataframe.describe() would not print anythink. Therefore I need to call
print(dataframe.describe()).
This leads to a totally different formatting though:
VSCode JupyterNotebook printing dataframe.describe() with print function
Is there a way to print dataframe.describe() in the first format?
There are multiple things to say here:
Jupyter Notebooks can only print out one object at a time when simply calling it by name (e.g. dataframe). If you want, you can use one cell per command to get the right format.
If you use the function print, it will print anything as text because print is using the function to_string for any object it gets. This is python logic - in contrast, option 1) is Jupyter-specific...
If you don't want to use a seperate cell and still get the right formatting, there are several options, one might be this:
from IPython.display import display
display(dataframe)
I assume you are using the same jupyter notebook files in both environments.
If that is the case, the problem you are facing is related to the execution order of the steps defined in the notebook itself, as the output of the code cell itself will be the one of the execution of the last line of the cell.
Let me illustrate you with an example.
Having the defined the following a dataframe in pandas:
data = {'id':[1,2,3,4],'nome':['Paolo','Pietro','Giovanni','Maria'],'spesa':[23.4,34.5,99.2,50.1]}
The output of the following cell would be different between this two cases:
# Outputs the dataframe itself
dataframe1 = pd.DataFrame(data)
dataframe1.describe()
dataframe1
# Outputs the describe() function return value
dataframe1 = pd.DataFrame(data)
dataframe1
dataframe.describe()
Both cells execute the same two lines on the dataframe without changing its internal state, however, only the last line will be written to the cell output.
I'm writing python using jupyter notebook and I have two cells that can influence each other.
I'm wondering is it possible to leave some certain cells out after I click Restart & Run All so that I can test the two cells independently?
One option based on Davide Fiocco's answer of this post and that I just tested is to include %%script magic command on each cell you don't want to execute.
For example
%%script false --no-raise-error
for i in range(100000000000000):
print(i)
If you put those two cells at the end of the page, you can run all cells above a certain cell with a single click.
That or you can put a triple-quote at the beginning and end of the two cells, then un-quote the cells to test them.
One option is to create a parameter and run the cells accordingly
x = 1
# cell 1
if x == 1:
// run this cell
# cell 2
if x != 1:
// run the other cell
In this example, you will skip cell 2.
I recently discovered an easy way to do this.
You may have noticed that cells can be set as type Code or Markdown - this lets you prepare the notebook with headers and explanatory text (in Markdown), but also sections of executable code (the default). This can be set from a drop-down already on the screen if using via Jupyter Lab. In Jupyter Notebook I think it's under the Cells menu.
You can also use keyboard shortcuts (first hit Escape if needed to get out of text-entry mode: Y for Code, Mfor Markdown, or Rfor Raw.
Wait, what's that about Raw? It appears to just take away the code highlighting and make the cell not executable! So Esc+R to make it Raw, then execute like you wanted to, then Esc+Y if you want to re-enable that block.
Alternative: If you want a quicker way to comment out all the lines but leave it as a Code block, make sure you are in edit mode (click on the cell contents), do Ctrl+A (for select-all), and then Ctrl+/ (for "comment this line"). I tested with python and it inserts # at the beginning of each selected line.
I have the following set of code which is trying to find the text in a cell within an excel worksheet called 'Input'. I am using openpyxl.
The cell uses a defined name in excel called 'Rperiod'. I can call the text by specifying the sheet and cell range directly, but I'm wondering if there is a way to use the defined_names function to keep it dynamic.
Rperiod = wb.defined_names['Rperiod']
Rperiod.value
This results in 'Input!$F$8' but then I can't workout if it is possible to use this result to get the text. The static method is:
input_sheet = wb.get_sheet_by_name('Input')
Rperiod_cell = input_sheet['F8']
Rperiod_cell.value
This returns the correct result 'Quarterly' but I obviously want to do this without directly specifying the cell 'F8' or the sheet 'Input'.
Any help is greatly appreciated!
I am trying to use xlwing's sheetname.range("Some object defined range").formula = "=Some excel formula" function, but I am not sure about how I can reference an object defined range as a string within the formula above.
I was able to define the object range as follows:
X = sht0.range('C10').expand('down').value
For all values within this range, I would like to perform a calcuation using an excel formula and I would like to use the following function.
sht0.range('F1').formula = "=SUM(X)"
It's a simple Sum function that could work if I just use the sum(X) function in python and use the result in the equation above without creating a string, but what if I would like to use any other built in excel functions instead?
One potential work around is using the xw.api syntax to create a "Named Range" in excel using the pywin32 syntax, but I can't seem to figure out what the syntax conversion would be.
As an example, in VBA it's:
Range("D7").Select
ActiveWorkbook.Names.Add Name:="SomeNameforaRange", RefersToR1C1:="=Sheet1!R7C4"
I tried the following in xlwings for python along with a few other attempts in the syntax:
sht0.range('C10').expand('down').api.Names = "SomeNameforaRange"
...but that doesn't work. It would just freeze my notebook or gives me an error.
Sorry if this question is very basic, I am a bit new to Python and xlwings, but I appreciate any help you may be to give.
Is it possible to execute the previous cell from in jupyterlab ?
something like:
import IPython
IPython.exec_cell(-1)
I want to execute the previous cell (if it has been updated without having been re-executed).
I have a cell that contains the test from the previous. If I modify the function, I want to only execute the cell that contains the test.