Skip to content

Instantly share code, notes, and snippets.

@sudodo
sudodo / create_github_issues_from_json.py
Created January 5, 2024 23:28
create Github issues from JSON
import requests
import json
import os
# Set your personal access token
token = os.environ.get('GITHUB_TOKEN')
your_username = 'sudodo'
your_repo = 'your_repo'
# Replace with the appropriate URL for your repository's issues
@sudodo
sudodo / vscode-latex.md
Created November 29, 2023 23:20 — forked from Ikuyadeu/vscode-latex.md
VSCode でLatexの日本語環境を作る

https://github.com/James-Yu/LaTeX-Workshop/wiki/Compile に書いてあったbibのコンパイルを日本語に対応.

  1. VSCodeやLatexをインストールしてなければインストール(https://code.visualstudio.com/
  2. Latex-Workshopをインストール(https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop)
  3. settings.json(Windows: ファイル > 基本設定 > 設定 or Ctrl + ,, Mac: Code > 基本設定 > 設定 or + ,)の{ }内に以下を追加(設定ファイルの変更方法: https://qiita.com/y-w/items/614843b259c04bb91495)
    "latex-workshop.latex.tools": [
        {
            "command": "ptex2pdf",
            "args": [
@sudodo
sudodo / logout.md
Created October 16, 2023 05:33
Logout from React Flask app

Implementing a logout feature in your web application involves invalidating the user's session or token on both the client and server sides. Since you're using token-based authentication (JWT), the process is mostly client-side because JWTs are stateless; they aren't stored on the server (unless you have a blacklist mechanism in place). Here's how you can implement the logout functionality:

1. Frontend (React):

In your React application, "logging out" a user typically involves removing the token from localStorage (or wherever it's stored) and updating the application's state accordingly.

Here's an example of what the logout button's click handler might look like:

const logoutUser = () => {
@sudodo
sudodo / auth_react_flask.md
Created October 16, 2023 05:23
Login authentication for React & Flask app

To ensure that only logged-in users can see the update_user page, you'll need to implement authentication checks. This typically involves sending the token (that you've stored in localStorage after login) with each request to your backend and verifying it there. Here's how you can achieve this:

  1. Send Token in Request from React: Modify your API calls to include the authentication token in the headers. With Axios, it might look something like this:

    import axios from "axios";
    
    const token = localStorage.getItem('token');
@sudodo
sudodo / create_db_flask.md
Created October 15, 2023 01:22
Create DB tables in Flask app (resolving circular import issue)

When working with Flask, circular imports (like importing db from app in models.py and User from models in app.py) can sometimes cause problems. To address this, you might want to create a separate file, say database.py, where you define and configure the SQLAlchemy instance, and then import db from this new file wherever it’s needed.

Example with a separate database.py file:

database.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
@sudodo
sudodo / how_to_migrate_flask_db.md
Created October 14, 2023 23:36
Migrate Flask DB

Question

When a developer update model classes after executing db.create_all(), can she reflect those updates into DB schema using convenient tools like db.create_all()?

Answer

When the database model classes (schemas) in a Flask application are updated, reflecting these changes in the actual database schema can be a bit tricky, especially when the database already contains data. db.create_all() is quite limited in this respect, as it does not modify existing tables - it only creates new tables if they do not exist.

For handling changes or migrations of the database schema, Flask has an extension called Flask-Migrate, which is built on top of another package called Alembic. Flask-Migrate/Alembic can generate migration scripts automatically based on the changes detected in the models. It also provides a command-line interface to apply the migrations to the database. Here's a basic flow of how it works:

@sudodo
sudodo / set_environment variables.md
Created October 14, 2023 20:40
How to set environment variables.

Setting environment variables can be slightly different depending on your production environment and platform (e.g., Linux server, Vercel, AWS, etc.). However, I'll guide you through a general approach and provide some platform-specific examples.

General Approach: Linux Server

If you're deploying your React app on a basic Linux server, you can set environment variables in various ways:

  1. Directly in Your Session: When you SSH into your server, you can set an environment variable that lasts for the duration of the session.

export REACT_APP_API_URL="https://yourapiurl.com"

@sudodo
sudodo / initiate_flask_app.py
Created October 13, 2023 09:26
Create new blank Flask app files.
import os
# Define the directory and file structure
structure = {
'myflaskapp': {
'app': {
'templates': {},
'static': {
'css': {},
'js': {},
@sudodo
sudodo / pdf_page_del_ocr.ipynb
Created May 19, 2023 07:35
pdf_page_del_ocr.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sudodo
sudodo / faster_inverse_matrix.py
Created September 24, 2016 02:34
Fast inverse matrix
def faster_inverse(A):
b = np.identity(A.shape[1], dtype=A.dtype)
n_eq = A.shape[1]
n_rhs = A.shape[1]
pivots = np.zeros(n_eq, np.intc)
identity = np.eye(n_eq)
def lapack_inverse(a):
b = np.copy(identity)
pivots = zeros(n_eq, np.intc)