Why does Streamlit not find my python file? - python

I have Streamlit working in terminal i.e. the following runs in terminal:
$ streamlit hello
I am trying to create an app with the online tutorial but encounter an error - see below
https://docs.streamlit.io/en/latest/tutorial/create_a_data_explorer_app.html#let-s-put-it-all-together
I have saved the following in as uber_pickups.py
import streamlit as st
import pandas as pd
import numpy as np
st.title('Uber pickups in NYC')
(base) lf-mac-0250:~ alastairhayes$ streamlit hello
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://172.20.10.2:8501
^C Stopping...
(base) lf-mac-0250:~ alastairhayes$ streamlit run uber_pickups.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]...
Error: Invalid value: File does not exist: uber_pickups.py
Where am I going wrong?
I have python 3.7.6
Many thanks!

If you're using Windows OS, you may try the steps below:
First, you need to download Anaconda:
https://www.anaconda.com/
Open the Anaconda PowerShell Prompt and type the following:
conda list
pip uninstall streamlit
Then, create a new environment, and test out the following:
pip install streamlit
streamlit hello
Finally, you can test out streamlit on your code, replace your_app with the actual name of your file. Make sure you are in the same directory as your source code file.
streamlit run your_app.py

Related

Azure dataset .to_pandas_dataframe() error

I am following an azure ml course on udemy and cannot get around the following error:
Execution failed in operation 'to_pandas_dataframe' for Dataset(id='id', name='Loan Applications Using SDK', version=1, error_code=None, exception_type=PandasImportError)
Here is the code for Submitting the Script:
from azureml.core import Workspace, Experiment, ScriptRunConfig,
Environment
ws = Workspace.from_config(path="./config")
new_experiment = Experiment(workspace=ws,
name="Loan_Script")
script_config = ScriptRunConfig(source_directory=".",
script="180 - Script to Run.py")
script_config.framework = "python"
script_config.environment = Environment("conda_env")
new_run = new_experiment.submit(config=script_config)
Here is the Script being run:
from azureml.core import Workspace, Datastore, Dataset,
Experiment
from azureml.core import Run
ws = Workspace.from_config(path="./config")
az_store = Datastore.get(ws, "bencouser_sdk_blob01")
az_dataset = Dataset.get_by_name(ws, name='Loan Applications Using SDK')
az_default_store = ws.get_default_datastore()
#%%----------------------------------------------------
# Get context of the run
#------------------------------------------------------
new_run = Run.get_context()
#%%----------------------------------------------------
# Stuff that will be logged
#------------------------------------------------------
df = az_dataset.to_pandas_dataframe()
total_observations = len(df)
nulldf = df.isnull().sum()
#%%----------------------------------------------------
# Complete the Experiment
#------------------------------------------------------
new_run.log("Total Observations:", total_observations)
for columns in df.columns:
new_run.log(columns, nulldf[columns])
new_run.complete()
I have run the .to_pandas_dataframe() part outside of an experiment and it worked without error. I have also tried the following (that was recommended in the driver log):
InnerException Could not import pandas. Ensure a compatible version is installed by running: pip install azureml-dataprep[pandas]
I have seen people come across this before but I cannot find a solution, any help is appreciated.
When doing an experiment a new azure environment was created without pandas installed. To install pandas (if using anaconda nav) go onto environments in the anaconda nav window, click the azure env, go to uninstalled packages and search pandas, click install. It worked once this was done.
For VS code users:
conda info --envs
You will have an environment with a name starting from azureml_f3f7e6c5xxxxxxx. Activate this environment
conda activate azureml_f3f7e6c5xxxxxxx
Then install pandas in the environment
pip install pandas

Is there a way to use Kaggle notebooks on the go?

I've been using the site Kaggle to take some courses on AI, but whenever I try to download one of the exercises and run the code within VS Code, it doesn't work. I will always get an error like this:
<ipython-input-1-76a2777bc721> in <module>
1 # Set up feedback system
----> 2 from learntools.core import binder
3 binder.bind(globals())
4 from learntools.ethics.ex4 import *
5 import pandas as pd
ModuleNotFoundError: No module named 'learntools'
Is there any way to circumvent this error so I can use Kaggle notebooks on the go?
Step1. Install Docker
Follow the link to install docker in your machine
https://docs.docker.com/engine/install
Step2. Download the relevant Docker-file/Docker image.Here in this case
use docker pull kaggle/python
Step3: Launch docker container from the folder where you have the notebook using the command below.
docker run -v $PWD:/src -p 8888:8888 --rm -it kaggle/python jupyter notebook --no-browser --ip="0.0.0.0" --notebook-dir=/src
Step4: Copy the url from the terminal and paste it in your browser.
The url looks something like this http://127.0.0.1:8888/?token=xxxxxxxxx

Run pip in python docker

I am completely new to docker (on windows 10 machine). I intend to setup a python development environment as a docker container. And most of the reading that I did involved the use of Dockerfile. I want to do it from scratch instead purely using commands.
What I intend to do is very basic requirement: To have python docker image present with me and that I should be able to install more libraries in that image and commit these updates to that image. But I want to do it completely using commands (not via a Dockerfile).
I am using Docker Desktop on windows 10 machine. I did docker pull python:latest and it pulled the image like so:
C:\Users\MyHomeDirectory>docker pull python:latest
latest: Pulling from library/python
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
532f6f723709: Pull complete
1334e0fe2851: Pull complete
062ada600c9e: Pull complete
aec2e3a89371: Pull complete
1ec7c3bcb4b2: Pull complete
Digest: sha256:65367d1d3eb47f62127f007ea1f74d1ce11be988044042ab45d74adc6cfceb21
Status: Downloaded newer image for python:latest
docker.io/library/python:latest
Then I did docker images and it showed that python latest image is present with a size of 886 MB.
C:\Users\Tejas.Khajanchee>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest 5b3b4504ff1f 47 hours ago 886MB
I am also able to enter the interactive python by doing docker run -it python and it generates the interactive shell:
Python 3.9.5 (default, May 12 2021, 15:26:36)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import gc
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>>
>>> import openpyxl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'openpyxl'
>>>
But as evident, some of the libraries are not installed. But this is where I get stuck. How do I install libraries into the python image and have the image updated. Also, if this shell is the only thing that I am allowed to do till now, what does the 886 MB content represent? Also I want to be able to run scripts using this docker image. When I attempt to do this on a very basic hello world script, the following error comes up:
C:\Users\MyHomeDirectory\Downloads>docker run -it python a.py
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "a.py": executable file not found in $PATH: unknown.
I want to be able to do this purely with commands and not a Dockerfile. Please help.
First, looks you confuse the concept of image & container.
Docker image: read only, used as basis of container
Docker container: overlay a writeable layer upon the read only layer of docker image, all container will use image as basis
Second, for you, you mentioned you want to install numpy in the image, the best way for this is to customized a Dockerfile like next:
Dockerfile:
FROM python
RUN pip install numpy
Then, build a new image with docker build -t newpython .
BUT, you mentioned you don't want to use Dockerfile, then the replacement is next:
Install numpy in a container:
docker run -it python /bin/bash
# pip install numpy
Use docker ps -a to get the container id, e.g: 0a6b4df8e2c2, then commit this container which already have numpy installed to a new image:
docker commit 0a6b4df8e2c2 newpython
Finally, all new container need to run base on newpython image not python image, as only the newpython image has numpy installed:
docker run --rm newpython python -c "import numpy; print(numpy.__version__)"
1.20.3
Additional, for docker run -it python a.py, I think you misunderstand the concept. Container command like python a.py means the command will executed in container, so the a.py should be in container, not in host machine.

Unable to set-up Pyspark

I have installed Pyspark and Findspark using conda environment and added their paths to environment variables.
I execute following code:
import findspark
import pyspark
findspark.find()
I get the output as:
'C:/Users/myname/AppData/Local/Continuum/anaconda3/Scripts'
Then I execute:
findspark.init("C:/Users/myname/AppData/Local/Continuum/anaconda3/Scripts")
The output I get is:
Please use a Docker container https://github.com/jupyter/docker-stacks/tree/master/pyspark-notebook and save yourself the trouble.

Running containerized PyTest

I am learning how to run containerized PyTests and I am failing to run a test with arguments.
My Dockerfile looks like this:
FROM python:2
ADD main.py /
RUN pip install docker
RUN pip install fake_useragent
RUN pip install pytest
RUN pip install requests
CMD ["pytest", "main.py --html=report.html"]
But I tried all kinds of CMD/RUN variations I found online.
Anybody has a clue?
The full project is here if helps:
https://github.com/pavelzag/DockerSDKLearn
"main.py --html=report.html" will be passed in pytest as a single argument and will appear in sys.argv[1] there. Hence pytest is trying to locate a file with the exact same name with stuff like --html in it. You should fully tokenize the command:
CMD ["pytest", "main.py", "--html=report.html"]

Categories