Markdown to LaTeX in Python - python

There are few versions of the Markdown-Latex package for Python, but I can't get any to work with the current version of the Markdown package. Does anyone have a working example using python like:
lines = markdown.markdown(lines,extensions=[MarkdownLatex()]
Thanks!

You can rename the file of the extension to "mdx_latex.py" and then, in the same directory, you can run the following command:
import markdown
md = markdown.Markdown(extensions=['latex'])
out = md.convert(text)

Related

How to create a library in Python that available to be called in prompt?

I need to create a simple library that will work in that way:
I run python command prompt interpreter from Windows Start
Write import <a name of library>
Then write m_relation(<arguments>)
Interpreter shows a result of what I need
I already have written down the code I need.
firstly you can make a folder for your package in this path:
C:\Users\youruser\AppData\Local\Programs\Python\Python310\Lib\site-packages\
for example new_folder,
then you make or paste your library in this path,
after that you should make a setup.py file in this folder that contain this sample code:
from setuptools import setup
setup(
name='My First Setup File',
version='1.0',
scripts=['your_file.py'],
)
then you open cmd in this path and run :
python setup.py install
eventually you can use this library from cmd.

Integrating R code via rpy2 into a Python package

I'm attempting to build a Python package, and use rpy2 and a handful of R scripts to integrate R seamlessly into that package.
This is code that I've prototyped previously in a Jupyter notebook. What this usually looks like is:
import rpy2
# load in R script containing some useful functions
rpy2.robjects.r("source('feature.R')")
# generate a python binding for 'useful_func' described in the R script
useful_func = rpy2.robjects.globalenv['useful_func']
result = useful_func(data)
This has worked well in Jupyter, as long as all my R scripts are in the same directory as the notebook I'm working with.
The package I'm trying to build looks something like:
package/
-__init__.py
-package.py
-lib/
-__init__.py
-feature1.py
-feature1.R
I can import feature1 easily, but when it tries to source feature1.R, R can't find the file. I can fix this by providing an absolute path to feature1.R but obviously this won't work when I attempt to distribute the package. How can I generate an absolute path to a resource file within a package in a way that is zip-safe?
...and I figured it out. Answering in case other folks have a similar form of this issue.
In feature1.py:
import importlib.resources as pkg_resources
import rpy2
with pkg_resources.path('lib', 'feature1.R') as filepath:
rpy2.robjects.r("source('" + str(filepath) + "')")
useful_func = rpy2.robjects.globalenv['useful_func']
You have resolved yourself the issue with the path in your package. The following is only a mention of convenience code in rpy2 to let you automagically map your R source file to a Python module (just like rpy2's importr() does, but without the need to have the R code in an R package):
https://rpy2.github.io/doc/v3.1.x/html/robjects_rpackages.html#importing-arbitrary-r-code-as-a-package

Retrieving the requirements of a Python single script

I would like to know how to extract the requirements from a single python script. I tried the following way, at the beginning of my file, immediately after the imports:
try:
from pip._internal.operations import freeze
except ImportError: # pip < 10.0
from pip.operations import freeze
x = freeze.freeze()
for p in x:
print(p)
The piece of code above, however, gives me back all the Python frameworks installed locally. I would like to extract only the necessary requirements for the script, in order to be able to deploying the final application.
I hope I was clear.
pipreqs is simple to use
install:
pip install pipreqs
in linux in the same folder of your script
use:
pipreqs .
then the requirements.txt file is created
pip home page:
https://pypi.org/project/pipreqs/
You can do this easily with 'modulefinder' python module.
I think you want to print all the modules required by a script.
So, you can refer to
http://blog.rtwilson.com/how-to-find-out-what-modules-a-python-script-requires/
or for your ease the code is here:
from modulefinder import ModuleFinder
f = ModuleFinder()
# Run the main script
f.run_script('run.py')
# Get names of all the imported modules
names = list(f.modules.keys())
# Get a sorted list of the root modules imported
basemods = sorted(set([name.split('.')[0] for name in names]))
# Print it nicely
print ("\n".join(basemods))

openpyxl not working as EXE

Error message from running my exe:
ModuleNotFoundError: No module named 'openpyxl'
testHi.py
#simple test to see if openpyxl module works
import openpyxl
print ("hi")
input()
hook-openpyxl.py
# taken from pyinstaller dev team, store in same dir as testHi.py
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('openpyxl')
cmd line input:
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
I run the the hiTest and get the error above.
I have looked everywhere for this solution. Can anyone tell me what I am doing wrong.
I fixed my issue by installing it through Pip, rather than install the package through Pycharm, and Pyinstaller was able to find the package.
I got the idea from looking through the text in the command prompt and saw it was loading modules that I had installed via Pip and not through Pycharm.
I was able to get this working using auto-py-to-exe (which uses pyinstaller) by including the following folders/files from my python library into the same folder that I run pyinstaller from:
jdcal.py
openpyxl (folder)
et_xmlfile (folder)
pyinstaller command:
pyinstaller -y -F "[directory]/myscript.py"
Notes on Library Location:
Windows library location for me was: C:\users[username]\AppData\Local\Programs\Python\Python37-32\Lib
The packages were in the "site_packages" folder
use
--hiddenimport openpyxl
long with the previous solutions, it worked for me, I was able to enforce the import of openpyxl.
You was quite close. :-)
I fixed the problem by modifying the "hook-openpyxl.py" file. The command collect_data_files('openpyxl') actually returns an empty list. But there is another command collect_submodules which seems to do what we want. So my "hook-openpyxl.py" file looks like this.
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('openpyxl')
I placed the "hook-openpyxl.py" file in the same directory like my spec file. In the spec file I set to location of the new hook file
a = Analysis(
...
...
hookspath=['.'],
...
...
...
I guess, you will have the same result with your command line parameter
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
My environment
Python=3.8
openpyxl=3.0.10
PyInstaller=4.8

How to build a distribution file?

I have created the setup.py file. But I don't know how to make a distribution?
The book said that Open a terminal window and type a single command: python3 setup.py sdist. I did that but always get error!
I feel the code is ok, because it is just the example in the book. I guess the error is from the way I build the distribution.
In the python command line terminal, how to change directory?
The code of file nester.py goes like following:
def print_lol(the_list):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)
The setup.py file is as following:
from distutils.core import setup
setup(
name = 'nester',
version = '1.0.0',
py_modules = ['nester'],
author = 'hfpython',
author_email = 'hfpython#headfirstlabs.com',
url = 'http://www.headfirstlabs.com',
description = 'A simple printer of nested lists',
)
And the command I type in is: python3 setup.py sdist then I get the error:
File "<stdin>",line 1
pythons setup.py sdist
syntaxError: invalid syntax
In the python command line terminal, how to change directory?
Irrelevant, since this isn't supposed to be entered into the Python REPL. Enter it into the shell/command prompt instead.
Make sure you add the path if Python is not installed in your c drive, and add the Python file name correctly (the python file is named automatically according to its version while installed). Also make sure the setup.py file is inside the Python file.
For example, I installed my python in e drive and the version is 3.4.2. The file name created automatically for it is Python34.
in the command line, I typed:
e:\Python34\setup.py sdist
Hope this help.
Joey
Since you are on windows, so depending upon the python version & install location of python
use command as like.
First you must be clear of the path to python.
C:\python33\python setup.py sdist
I hope this helps.

Categories

Resources